我正在尝试在我的 React Redux 应用程序中使用 Firebase 进行身份验证。 我想在成功登录后将用户重定向到下一页,但在提交登录表单时收到错误
错误
Cannot read property 'then' of undefined
处理LoginForm中的提交功能
handleFormSubmit(event) {
event.preventDefault();
if (this.props.email !== '' && this.props.password !== '') {
this.props.loginUser(this.props.email, this.props.password)
.then(() => {
this.context.router.push('/posts');
});
} else {
console.log('empty');
}
}
Action 创建器
export function loginUser(email, password) {
return (dispatch) => {
dispatch({ type: LOGIN_USER });
firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => loginUserSuccess(dispatch, user))
.catch(() => {
console.log('failed to sign in');
return;
});
};
}
export function loginUserSuccess(dispatch, user) {
dispatch({
type: LOGIN_USER_SUCCESS,
payload: user
});
}
如果我从句柄提交功能中删除 then,身份验证会起作用,但我不知道如何将用户重定向到下一页?
最佳答案
我能够通过在调度中包含返回来解决该问题。我将更新后的代码放在这里以供将来的访问者使用。
export function loginUser(email, password) {
return (dispatch) => {
dispatch({ type: LOGIN_USER });
return firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => loginUserSuccess(dispatch, user))
.catch(() => {
console.log('failed to sign in');
return;
});
};
}
谢谢。
关于javascript - 使用 React Redux Redux-Thunk 进行 Firebase 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45337972/