onAuthStateChanged不一致 [英] onAuthStateChanged inconsistent

查看:143
本文介绍了onAuthStateChanged不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户访问我的网站时,我使用 onAuthStateChanged 来确定用户是否已经从前一个会话登录过。



主要的问题在于,对于一​​小部分用户而言,它并不一致 - 代码似乎没有检测到他们有一个有效的登录会话正在进行。

以下代码位于应用程序本身,用户在页面或登录页面重定向后即可在该页面上显示。



该站点还使用 onAuthStateChanged 来确定用户是否应直接重定向到应用程序。登录页面使用signInWithEmailAndPassword,当登录成功时,将其重定向到应用程序页面(具有以下代码)。

  firebase.auth()。onAuthStateChanged(function(authUser){
if(authUser)return initApp(authUser);

//问题:有时用户需要登录在这里匿名签名
firebase.auth()。signInAnonymously()。then(function(authUser){
initApp(authUser);
}
});

onAuthStateChanged 只是不应该用于我的使用案例?任何想法如何改善/解决这个问题?
$ b 编辑:这个SEEMS发生在手机上的用户更多。

解决方案

也许你应该使用 JavaScript Cookie e.auth.Auth#currentUserrel =nofollow> Firebase.Auth.currentUser )
$ b 2)将令牌保存为cookie


$ b $ 3)在页面加载时,首先找到cookie
$ b <4>如果存在cookie,请调用 signInWithCustomToken 函数

5)如果会话超时,请再次调用 signInWithCustomToken

When a user comes to my site, I use onAuthStateChanged to determine if the user is already signed-in from a previous session or not.

Main issue is that it has been inconsistent for a small % of users - code seems to not be detecting that they do have a valid signed-in session going on.

The following code is on the app itself, users are on that page after a redirect from the homepage or login page.

The homepage of the site also uses onAuthStateChanged to determine if the user should be redirected to the app directly. The login page uses signInWithEmailAndPassword and when the log in is successful, redirects them to the app page (which has the following code).

firebase.auth().onAuthStateChanged(function (authUser) {
  if (authUser) return initApp(authUser);

  // issue: sometimes users that *should* be signed-in get signed in anonymously here
  firebase.auth().signInAnonymously().then(function (authUser) {
    initApp(authUser);
  }
});

Is onAuthStateChanged just not something I should be using for my use case? Any idea how to improve / fix this?

Edit: This SEEMS to happen more with users on mobiles.

解决方案

Maybe you should use JavaScript Cookies

1) If user sign in,get the token (Firebase.Auth.currentUser)

2) Save the token to cookies

3) On the page load, find the cookie first

4) If cookie exist, call the signInWithCustomToken function

5) If session timeout, call the signInWithCustomToken again

这篇关于onAuthStateChanged不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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