简体   繁体   English

刷新页面时,有时firebase.auth()。onAuthStateChanged返回null的用户

[英]Sometimes firebase.auth().onAuthStateChanged returns user of null when I refresh the page

Precondition 前提

$npm install --save firebase@4.11.0 $ npm install-保存firebase@4.11.0

issue 问题

I'm using firebase authentication on my web application. 我在Web应用程序上使用Firebase身份验证。 In my app, I implemented onAuthStateChanged for client side js like below. 在我的应用中,我为客户端js实现了onAuthStateChanged ,如下所示。

firebase.auth().onAuthStateChanged((user) => {
  if(user) {
    //logged in
  } else {
    //do sth
  }
});

After login, I confirmed this method will return actual user obj, but if I refresh the page, then user might be null. 登录后,我确认此方法将返回实际用户obj,但是如果刷新页面,则user可能为null。 Curiously, sometimes user won't be null. 奇怪的是,有时用户不会为空。 I'm afraid there are some limitation of calling onAuthStateChanged, but currently I have no idea. 恐怕调用onAuthStateChanged有一些限制,但是目前我不知道。

How should I deal with this issue? 我应该如何处理这个问题?

update 更新

Let me share my minimal example. 让我分享我的最小例子。

My app is working with express.js. 我的应用程序正在使用express.js。 There are two URLs like below. 有如下两个URL。 /login /main /login /main

In the login page, I implemented authentication method. 在登录页面中,我实现了身份验证方法。 If the login is successfully finished, then user will be redirected to '/main'. 如果登录成功完成,则用户将被重定向到“ / main”。

//login.js
import firebase from 'firebase';
var config = {...};
firebase.initializeApp(config);

var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider)
.then((result) => {
  return result.user.getIdToken(true);
}).then((idToken) => {
  if(idToken) {
    location.href = '/main';
  }
});

In the main page, there is no login method. 在主页上,没有登录方法。 main.js is only checking whether user is logged in. main.js仅检查用户是否已登录。

//main.js
import firebase from 'firebase';
var config = {...};
firebase.initializeApp(config);

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    //initialize main page.
  } else {
    location.href = '/login';
  }
}

I think login status is stored on LocalStorage of web browser. 我认为登录状态存储在Web浏览器的LocalStorage中。 This means that, after finishing loading of main.js, onAuthStateChanged will be automatically fired with user information, but not working as I expected. 这意味着,在完成main.js的加载后,onAuthStateChanged将自动使用用户信息触发,但无法正常工作。 I'm sure that persistence of login information is correct because official document says the default setting is LOCAL for web client. 我确信登录信息的持久性是正确的,因为官方文档说Web客户端的默认设置为LOCAL

https://firebase.google.com/docs/auth/web/auth-state-persistence https://firebase.google.com/docs/auth/web/auth-state-persistence

my question 我的问题

Should I implement onAuthStateChanged with another way? 我应该以其他方式实现onAuthStateChanged吗? How can I ensure user is logged in after reload? 重新加载后如何确保用户登录?

eg 例如

import $ from 'jquery';
$(document).on('ready', () => {
  onAuthStateChanged((user) => {...});
});

Or could you show me the correct way? 或者您能告诉我正确的方法吗?

Workaround 解决方法

I decided to remove session and set redirection to login page if null is returned. 我决定删除会话,如果返回null,则将重定向设置为登录页面。 This is not a solution, but a workaround currently... 这不是解决方案,而是当前的解决方法...

You're not calling onAuthStateChanged . 您不是在调用onAuthStateChanged Instead you're telling Firebase to call you when the authentication state changes, which may happen a few times when the page is being re-loaded 相反,您是告诉Firebase在身份验证状态更改时打电话给 ,这可能在重新加载页面时发生几次

When a page is getting loaded and there was previously a user signed in, the auth state may change a few times, while the client is figuring out if the user's authentication state it still valid. 当页面被加载并且以前有用户登录时,身份验证状态可能会更改几次,而客户端正在确定用户的身份验证状态是否仍然有效。 For that reason, you may see a call with no user before seeing the final call with the actual signed in user. 因此,在看到具有实际登录用户的最终呼叫之前,您可能会看到没有用户的呼叫。

The fact it's sometimes null and sometimes not null likely points to an async problem. 它有时为null有时不是null的事实可能表明存在异步问题。 Are you making the check in the if statement above? 您是否要在上述if语句中进行检查? All references to the user should be within the callback. 对用户的所有引用都应在回调内。 If that all checks out, maybe check that authentication is being properly initiated. 如果全部检查完毕,则可能要检查身份验证是否已正确启动。

onAuthStateChanged is an observer as stated in firebase docs, which gets triggered when the auth state is changed like user signed in, signed out, pwd change. onAuthStateChanged是firebase文档中所述的观察者,当身份验证状态发生更改(例如用户登录,注销,密码更改)时,就会触发该观察器。 To check if user is logged in or not you should use firebase.auth().currentUser which will give you the current logged in user. 要检查用户是否已登录,您应该使用firebase.auth()。currentUser,它将为您提供当前的登录用户。 As you said your state is local firebase.auth().currentUser will always give you user unless user is signed out. 如您所说,您的状态为本地firebase.auth()。currentUser将始终为您提供用户,除非用户已注销。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase.auth().onAuthStateChanged 在 vuex 中刷新页面后获取 null - firebase.auth().onAuthStateChanged getting null after refresh page in vuex firebase.auth()。当刷新页面时,currentUser返回null - firebase.auth().currentUser returns null when page is refreshed "firebase.auth().onAuthStateChanged 不工作" - firebase.auth().onAuthStateChanged Not Working 类型错误:调用“firebase.auth().onAuthStateChanged((user)”时无法读取未定义的属性“auth” - TypeError: Cannot read property 'auth' of undefined when calling " firebase.auth().onAuthStateChanged((user)" firebase.auth()。currentUser和onAuthStateChanged始终为null,尽管已登录 - firebase.auth().currentUser and onAuthStateChanged always null inspite of being signed in 使用 firebase.auth().oAuthStateChanged 在页面刷新时获取令牌 null - Getting token null on page refresh using firebase.auth().oAuthStateChanged 我如何使用firebase.auth()。onAuthStateChanged()返回我的userID的值,它当前返回未定义的 - How can i return the value of my userID with firebase.auth().onAuthStateChanged(), it currently returns undefined 跨两个页面使用 firebase.auth().onAuthStateChanged 时的问题 - Issue when using firebase.auth().onAuthStateChanged accross two pages 在 React 中放置 firebase.auth().onAuthStateChanged 的位置 - where to place firebase.auth().onAuthStateChanged in React firebase.auth().onAuthStateChanged 无限循环 - firebase.auth().onAuthStateChanged looping infinitely
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM