Javascript 使用React-Redux-Redux-Thunk进行Firebase身份验证

Javascript 使用React-Redux-Redux-Thunk进行Firebase身份验证,javascript,reactjs,firebase,react-router,react-redux,Javascript,Reactjs,Firebase,React Router,React Redux,我正在尝试在我的react redux应用程序中使用firebase进行身份验证。 我想在成功登录时将用户重定向到下一页,但在提交登录表单时出现错误 错误 Cannot read property 'then' of undefined export function loginUser(email, password) { return (dispatch) => { dispatch({ type: LOGIN_USER }); firebase.auth().s

我正在尝试在我的react redux应用程序中使用firebase进行身份验证。 我想在成功登录时将用户重定向到下一页,但在提交登录表单时出现错误

错误

Cannot read property 'then' of undefined
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
  });
}
处理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');
    }
  }
动作创建者

Cannot read property 'then' of undefined
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-from-handle-submit函数,身份验证就会起作用,但我不知道如何将用户重定向到下一页?

我可以通过在调度中包含一个返回来解决问题。我把更新后的代码放在这里,供未来的访问者使用

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;
      });
  };
}

谢谢。

OP没有提到它,但是对于那些使用和它的人来说,它看起来是这样的:

export function loginUser(email, password) {
  return (dispatch, { getFirebase }) => {
    return getFirebase()
      .login({ email, password }
      .catch(() => {
        console.log('failed to sign in');
        return;
      });
  };
}

也就是说,如果您使用的是
firebaseConnect
withFirebase
,则应该能够在组件中直接调用login,如中所示。

是的,这就是我要发布的解决方案。你出错的原因是你没有回复承诺。