【问题标题】:Auth firebase onAuthStateChangeAuth Firebase onAuthStateChange
【发布时间】:2017-07-28 08:40:50
【问题描述】:

当用户成功登录时,我被重定向到我的主页(我想要的),但它一直在刷新。知道可能是什么原因吗?

代码:

firebaseApp.auth().onAuthStateChanged(user => {

    if(user){
        console.log('user is signed in', user);
        window.location='/';

    }else{
        console.log('user is NOT signed in');

    }
})

【问题讨论】:

  • 一直刷新是什么意思?
  • 太奇怪了,浏览器一直在刷新页面……就像 window.location='/' 每隔半秒就会被调用一次

标签: javascript firebase firebase-authentication


【解决方案1】:

猜测;可能是因为代码在页面加载后再次执行并不断重定向?如果是这种情况,您可以包装 if 或确保代码仅嵌入在登录页面中。

firebaseApp.auth().onAuthStateChanged(user => {

if(user){
    console.log('user is signed in', user);
    if(window.location.href != "/"){
        window.location='/';
    }

}else{
    console.log('user is NOT signed in');

}

})

【讨论】:

  • 是的,这是我的想法,但我会在 if 语句中添加什么?
  • logIn() { console.log('this.STATE:', this.state); const {电子邮件,密码} = this.state; firebaseApp.auth().signInWithEmailAndPassword(email, password).catch(error => { this.setState({error}) //登录成功时重定向到 /app 的 TODO 功能,失败时继续登录 }).then( function () { if(firebaseApp){ window.location = '/'; }else { window.location='/signin' } }) }
  • 我虽然如果我在我的代码前面放 5 个空格,它会将它识别为代码......我怎样才能在评论中做到这一点?提前谢谢
  • 您是否尝试过我编写的代码中的 if 语句?对于代码,我不相信你可以让它看起来与 cmets 中的缩进一样好,但如果你在 code 周围使用反引号,它将是相同的灰色。
  • 感谢您的反馈,上面的 onAuthStateChange 在我的 index.js 中。我试图将其移至我的 login.js,但结果相同 -> 不断刷新。我现在已经这样做了 -> ^ firebaseApp.auth().signInWithEmailAndPassword(email, password).catch(error => { this.setState({error}) //TODO 功能,用于在登录成功时重定向到 /app,请继续登录失败 }).then(function () { if(firebaseApp){ window.location = '/'; }else { window.location='/signin' } })^
猜你喜欢
  • 2021-08-09
  • 2017-04-29
  • 2019-08-17
  • 2019-10-24
  • 2016-12-29
  • 2021-06-27
  • 1970-01-01
  • 2021-05-14
  • 2017-09-07
相关资源
最近更新 更多