【发布时间】: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....
感谢大家会尽力帮助我。
【问题讨论】:
-
安装chrome redux dev扩展,这样你就可以看到谁在修改你的
auth分支root。因为有很多操作可以修改它,所以您可以查看何时以及谁以何种顺序执行了该操作。 chrome.google.com/webstore/detail/redux-devtools/…
标签: reactjs react-native redux firebase-authentication