首页 文章

Firebase登录似乎不起作用

提问于
浏览
1

我有这个React Redux Firebase应用程序 .

在商店中,我们有一个userReducer只有一个名为authed的状态属性,如果用户是否被记录则是一个布尔值 .

我有这个 config.js 文件,用于配置firebase功能和东西 .

export const ref = firebase.database().ref();

export const firebaseAuth = firebase.auth;

export function login (email, pw) {
  return firebaseAuth().signInWithEmailAndPassword(email, pw)
}

逻辑如下>在开始时authed值为false,然后我们使用创建帐户

export function auth(email, pw) {
  return firebaseAuth().createUserWithEmailAndPassword(email, pw)
    .then(saveUser);
}

然后在 App.js 我们有

componentDidMount() {
    this.removeListener = firebaseAuth().onAuthStateChanged((user) => {
      if (user) this.props.authed_true();
      else this.props.authed_false();
    })
  }

  componentWillUnmount () {
    this.removeListener()
  }

来自redux的 this.props.authed_true/false 函数只需更改状态的authed prop,具体取决于是否有用户 .

如果authed为true,意味着我们有一个用户有一个重定向到某个路由 .

现在让我们明白这一点 .

注册用户是完美的,但是当我尝试使用Login时

export function login (email, pw) {
  return firebaseAuth().signInWithEmailAndPassword(email, pw)
}

我得到了

错误:发生网络错误(例如超时,中断连接或无法访问的主机) .

我的网址栏变成了

http://localhost:3000/login

http://localhost:3000/login?email=anorak%40abv.bg&password=anorak97

那么为什么login()不起作用?有任何想法吗?

2 回答

  • 1

    操作应该是对象,但是 login 动作创建器函数正在返回异步处理的promise . 这是redux-thunk的一个很好的用例 . 使用该库,您可以将其更改为:

    export function login (email, pw) {
      return (dispatch) => {
        firebaseAuth().signInWithEmailAndPassword(email, pw)
        .then(user => dispatch({ type: SIGNIN_SUCCESSFUL, payload: user }))
        .catch(err => dispatch({ type: SIGNIN_ERROR, payload: err })
      }
    }
    
  • 1

    你的代码不好,你应该考虑推杆

    firebaseAuth().onAuthStateChanged((user) => {
      if (user) this.props.authed_true();
      else this.props.authed_false();
    })
    

    在中间件中,因为它是登录后的副作用,你可以试试这样的东西

    const loginMiddleware = store => next => (action) => {
      if (action.type === GET_USER_LOGIN) {
        login(action.payload.login, action.payload.pw);
        firebaseAuth().onAuthStateChanged((user) => {
          if (user) store.dispatch(authed_true());
          store.dispatch(authed_false())
        })
      }
      return next(action);
    };
    
    export default loginMiddleware;
    

    But 这不是您的问题,很可能您的标记中只有一个元素,并且在 Submit 后更改了URL,因此您应该尝试删除第一个

相关问题