Firebase身份验证-用户在Firebase注册并重定向后未登录 [英] Firebase Auth - User not logged in after Firebase sign up and redirect

查看:77
本文介绍了Firebase身份验证-用户在Firebase注册并重定向后未登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为具有Firebase的用户创建一个帐户.创建帐户后,我将重定向到主页,该页面检查我是否登录.很奇怪,我尚未登录.

I am creating an account for a user with firebase. After the account is created, I redirect to the home page, which checks if I am logged in. Oddly enough, I am not logged in.

在创建帐户"页面上:

createAccount() {
  firebaseAuth().createUserWithEmailAndPassword(this.state.email, this.state.password).then((user) => {
    // write some data to database
    firebase.database().ref().child('users').child(user.uid).child('someKey').set(this.state.email).then(function() {
      // send email verification
      user.sendEmailVerification().then(function() {
        console.log("sent email verification");
        // Go to home page
        this.props.history.push('/home');
      }.bind(this)).catch(function(error) {
        console.log("Error: account created but no verification email sent.");
      }.bind(this));
    }.bind(this)).catch((error) => {
      console.log('Error: while writing to database');
    });
  // catch any error while creating user
  }).catch((error) => {
    console.log('Error: while creating account');
  });
}

在首页上:

componentDidMount() {
  firebase.auth().onAuthStateChanged(function (user) {
    if (!user) {
      console.log("no user logged in");  
      this.props.history.replace('/login'); // ISSUE: Always redirected to login, even after signup and login
      return
    } else {
      console.log("Woo! User is logged in!");
    }
  }.bind(this))
}

替代尝试:

  • 我已尝试在注册后显式登录用户,但是会出现相同的结果
  • this.createAccount替换为onClick处理程序的() => this.createAccount(结果相同)
  • 使用firebase.auth()而不是firebaseAuth()(结果相同)
  • I have tried explicitly logging the user in after signup, but the same result occurs
  • replaced this.createAccount with () => this.createAccount for onClick handler (same result)
  • used firebase.auth() instead of firebaseAuth() (same result)

有人知道我为什么未登录吗?

** **

以下是我初始化应用程序的方式:

Below is how I am initializing my app:

<!-- Firebase -->
<script src="https://www.gstatic.com/firebasejs/4.0.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "XXXXXXXXX",
    authDomain: "myproject.firebaseapp.com",
    databaseURL: "https://myproject.firebaseio.com",
    projectId: "XXXXXXXXX",
    storageBucket: "XXXXXXXXX.appspot.com",
    messagingSenderId: "XXXXXXXXX"
  };
  firebase.initializeApp(config);
</script>

可能值得注意的是:如果我使用的帐户不是刚刚创建的(或者我稍等片刻然后登录到我创建的帐户),则保留似乎没有问题重定向时的授权.似乎只有当我创建一个新用户然后重定向时,它才会丢失已登录的用户.

It may be worth noting: If I sign in with an account that was not just created (or if I wait a while and then sign in to the account I created), there doesn't seem to be an issue with retaining the authorization when redirected. It seems to be that it's only when I create a new user and then redirect that it loses the logged in user.

推荐答案

我具有相同的设置,并且运行正常.

I have the same setup and it works fine.

我的路由器顶部有身份验证状态更改侦听器...

I have my auth state change listener at the top of my router...

state = { user: null };

componentDidMount() {
   firebase.auth().onAuthStateChanged(user => {
      if (user) {
        this.setState({ user });
      } else {
        this.props.history.push('/login');
      }
   });
 }

在Login.js中,我有一个表单可以连接到类似于您的登录函数...

In Login.js I have a form that connects to a login function similar to yours...

login() {
    firebase
      .auth()
      .signInWithEmailAndPassword(this.state.email, this.state.password)
      .then(res => {
        this.props.history.push('/');
      })
      .catch(err => console.error(error));
  }

我唯一想到的是您给了history.replace,请尝试更改为history.push.

The only thing that I can think of you gave history.replace, try changing to history.push.

如果这不起作用,您可以设置 https://codesandbox.io/使用mcve,我很乐意对其进行调试 https://stackoverflow.com/help/mcve

If that doesn't work is there any chance you can set up a https://codesandbox.io/ with a mcve and I'd be happy to debug it https://stackoverflow.com/help/mcve

这篇关于Firebase身份验证-用户在Firebase注册并重定向后未登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆