javascript - Firebase 登录似乎不起作用

标签 javascript reactjs firebase redux

我有这个 React + Redux + Firebase 应用程序。

在 store 中,我们有一个 userReducer,它只有一个名为 authed 的状态属性,它是一个用于跟踪用户是否登录的 bool 值。

我有这个 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()
  }

this.props.authed_true/false 来自 redux 的函数只是根据是否有用户更改状态的 authed prop。

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

现在让我们明白这一点。

注册用户工作完美,但当我尝试使用登录时

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

我得到一个

Error: A network error (such as timeout, interrupted connection or unreachable host) has occurred.

我的地址栏变成了

来自

http://localhost:3000/login

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

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

最佳答案

操作应该是对象,但是您的 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 })
  }
}

关于javascript - Firebase 登录似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48630764/

相关文章:

javascript - Gulp 文件与 browsersync 的集成

reactjs - React-Router 4默认路由内容显示在所有其他组件上

reactjs - 我正在尝试使用 React.createElement 动态创建 Mui 图标。但是,React 会将该元素小写。有办法保留案例吗?

javascript - 无法实现 mustache js 来显示信息

javascript - 如何让 div 始终用紧随其后的内容填充视口(viewport)(完全响应)?

javascript - 显示 :none; in select multiple doesn't work on IE 8

javascript - 使用查询字符串的终极搜索

android - 从 Firebase 视觉文本检测总量

node.js - 使用 Firebase Cloud Functions 实现 REST 接口(interface)

java - 如何返回 DocumentSnapShot 作为方法的结果?