【问题标题】:React-native redux localId is not changing from "null"React-native redux localId 没有从“null”更改
【发布时间】:2020-10-11 00:20:43
【问题描述】:

我正在用 React-native 构建一个应用程序,但我遇到了这个问题:

当我登录或注册到我的应用程序时,身份验证运行良好,但是当我尝试获取我的 userId 的状态时,例如:其他操作中的“getState().auth.userId”,结果为 null。

你能帮我找出问题吗?

这是 App.js

const rootReducer = combineReducers({
  friends : friendReducer,
  auth : authReducer
})


const store = createStore(rootReducer,applyMiddleware(ReduxThunk));

export default function App() {
  return (
    <Provider store={store}>
      
        <Navigator/>
      
    </Provider>
  );
}

身份验证器


const initialState = {
    token: null,
    userId: null
}

export default (state = initialState, action) => {
    switch (action.type){ 
        case SIGN_UP:
            return {
                token: action.payload.token,
                userId: action.payload.localId
            }
        case LOGIN:
            return {
                token: action.payload.token,
                userId: action.payload.localId
            }
        case LOGOUT:
            return initialState
        default:
            return state
    }
    
}

身份验证操作

export const SIGN_UP = 'SIGN_UP'
export const LOGIN = 'LOGIN'
export const AUTHENTICATE = 'AUTHENTICATE'
export const LOGOUT = 'LOGOUT'

export const authenticate = (localId, token, expiryTime) => {
    return dispatch => {
        dispatch(setLogOutTimer(expiryTime))
        dispatch({type: LOGIN, payload :{localId: localId, token: token}})
}}

export const sign_up = (userId, token, expiryTime) => {
    return dispatch => {
        dispatch(setLogOutTimer(expiryTime))
        dispatch({type: SIGN_UP, payload :{userId: userId, token: token}})
}}


export const signUp = (email,password) => {
    return async dispatch => {
        const response = await fetch(
            'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyDu59dDOXxR1OakrD2iWam2zhRSEOZdddc',
        {
            method: 'POST',
            headers: { 
                'Content-Type':'application/json'
            },

            body: JSON.stringify({
                email: email,
                password: password,
                returnSecureToken: true
            })

            
        })
        if (!response.ok){
                
            const errorData = await response.json()
            console.log(errorData)
            const errorId = errorData.error.message
            let message = 'something went wrong'
            if (errorId == 'EMAIL_EXISTS'){
                message= 'This email addess already exists!'
            }
            throw new Error(message)
    }

        const resData = await response.json()
        console.log(resData)

        dispatch(sign_up(
            resData.localId, 
            resData.idToken, 
            parseInt(resData.expiresIn )* 1000 ))
        const expiration = new Date(new Date().getTime() + parseInt(resData.expiresIn) * 1000)
        saveDataToStorage(resData.idToken, resData.localId, expiration)

    }
}


export const login = (email,password) => {
    return async dispatch => {
        const response = await fetch(
            'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDu59dDOXxR1OakrD2iWam2zhRSEOZdddc',
        {
            method: 'POST',
            headers: { 
                'Content-Type':'application/json'
            },

            body: JSON.stringify({
                email: email,
                password: password,
                returnSecureToken: true
            })
        })
        if (!response.ok){
                
                const errorData = await response.json()
                console.log(errorData)
                const errorId = errorData.error.message
                let message = 'something went wrong'
                if (errorId == 'EMAIL_NOT_FOUND'){
                    message= 'We can not find this email, sorry!'
                }else if (errorId == 'INVALID_PASSWORD'){
                    message = 'this password is not valid, sorry'
                }
                throw new Error(message)
        }
        const resData = await response.json()
        console.log('printing resData...')
        console.log(resData)
        console.log(resData.localId)
        dispatch(authenticate(resData.localId, resData.idToken, parseInt(resData.expiresIn) * 1000))

        const expiration = new Date(new Date().getTime() + parseInt(resData.expiresIn) * 1000)
        
        saveDataToStorage(resData.idToken, resData.localId, expiration)
    }
}

这是我试图从 auth reducer 获取 userId 的另一个操作(即对 Firebase 进行空检查数据库,所以我认为我的问题在这里......):

export const addFriend = friend => {
    return async (dispatch, getState) => {
        
        const userId = getState().auth.userId
        
        const response = await fetch(`https://placeyou-84d85.firebaseio.com/friends/${userId}.json`, {
            method: 'POST',
            headers: {
                'Content-Type' : 'application/json' },
            body: JSON.stringify({  ecc....

感谢大家会尽力帮助我。

【问题讨论】:

标签: reactjs react-native redux firebase-authentication


【解决方案1】:

非常感谢!!

我发现了问题并解决了。

问题是令牌的过期时间设置为3.6秒(我认为这是由于我之前做过的一些测试)。

【讨论】:

    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 2021-03-08
    • 2018-05-02
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2018-11-18
    相关资源
    最近更新 更多