无法在加载时获得currentUser [英] Can't get currentUser on load

查看:115
本文介绍了无法在加载时获得currentUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试检查用户是否通过 firebase.auth()。currentUser 登录时:

When trying to check if a user is signed in via firebase.auth().currentUser like this:

if (firebase.auth().currentUser === null) {
  console.log('User not signed in');
}

每当我刷新页面,或者在上面导航时都返回null(即使我刚刚登录)。

Whenever I refresh the page, or navigate around the above returns null (even though I have just logged in).

奇怪的是,如果我记录

console.log(firebase.auth().currentUser) // This returns null
console.log(firebase.auth()) // Here I can inspect the object and currentUser exists...!

我真的不知道这里发生了什么。我正在使用React和Redux,但我说的并不重要。

I don't really know what's going on here. I'm using React and Redux, but it shouldn't really matter I'd say.

firebase初始化并且你无法访问currentUser?如果是这样,我如何在 firebase.auth()的日志输出中看到它?

Is there a small delay where the firebase initialises and you can't access the currentUser? If so, how can I see it in the log output of firebase.auth()?

推荐答案

这是一个常见问题。
https://firebase.google.com/docs/auth/web / manage-users
您需要向onAuthStateChanged添加一个观察者来检测初始状态和所有后续状态更改,

This is a commonly asked question. https://firebase.google.com/docs/auth/web/manage-users You need to add an observer to onAuthStateChanged to detect the initial state and all subsequent state changes,

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

这篇关于无法在加载时获得currentUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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