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

查看:32
本文介绍了无法在加载时获取 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天全站免登陆