React-native redux localId没有从“null”更改

zpqajqem  于 2023-05-07  发布在  React
关注(0)|答案(1)|浏览(182)

我正在用React-native构建一个应用程序,但我遇到了这个问题:
当我登录或注册我的应用程序时,身份验证工作正常,但当我尝试获取我的userId状态时,例如:“getState().auth.userId”在其他操作中的结果为空。
能帮我找一下问题吗?
这是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数据库进行null检查,所以我想我的问题在这里...):

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....

谢谢大家都来帮助我。

8ftvxx2r

8ftvxx2r1#

非常感谢大家试图帮助我!!
我发现了问题并解决了它。
问题是令牌的过期时间被设置为3.6秒,我认为这是由于我之前做的一些测试)。正因为如此,我才犯了这个错误。

相关问题