简体   繁体   English

跨两个页面使用 firebase.auth().onAuthStateChanged 时的问题

[英]Issue when using firebase.auth().onAuthStateChanged accross two pages

I am having a small problem with two html pages which used to work in the past.我在过去可以使用的两个 html 页面上遇到一个小问题。

First, I have a login page (login.html) containing this kind of code:首先,我有一个包含这种代码的登录页面(login.html):

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    window.open("main.html",'_self');
  } else {
    // No user is signed in.
    prompt for login ....
    ......
  }
});

Second, I also have this page (main.html) containing this sort of code:其次,我也有这个页面(main.html)包含这种代码:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    do serious work ....
    ......
  } else {
    // Let us double check:
    let theUser = firebase.auth().currentUser;

    if (!theUser) {
        // No user is signed in.
        window.open("login.html",'_self');
    }
    // We need some patience :)
    ......
  }
});

Once a user is logged in, things are supposed to go to main.html;一旦用户登录,事情应该是 go 到 main.html; and this is how it used to work.这就是它过去的工作方式。 But now, for some unknown reason;但是现在,不知什么原因; when I log in, even though I have provided my credentials, the flow goes to main.html (as it should) but immediately bounces back to login.html.当我登录时,即使我提供了我的凭据,流程也会转到 main.html(应该如此),但会立即反弹回 login.html。

I have been able to check that the block (in the main.html):我已经能够检查该块(在 main.html 中):

firebase.auth().onAuthStateChanged(function(user) {...}

does not detect me as logged in. Why is that?没有检测到我已登录。这是为什么呢?

PS I have verified that my domain is added in the list of Authorized domains in the Firebase console. PS 我已验证我的域已添加到 Firebase 控制台的授权域列表中。

I am encountering the same issue.我遇到了同样的问题。 I narrowed it down to Chrome.我将其缩小到 Chrome。 It seems to think that Chrome isn't allowed to save cookies when it is (I checked the settings).似乎认为Chrome不允许保存cookies(我检查了设置)。 Might be an issue with a recent Chrome update?最近的 Chrome 更新可能有问题? Just as a sanity check, try it on Firefox and let me know if that works.作为健全性检查,在 Firefox 上尝试一下,让我知道它是否有效。 It did for me.它对我有用。

UPDATE: This is what I get back when I console log the error更新:这是我在控制台记录错误时得到的

{code: "auth/web-storage-unsupported", message: "This browser is not 
supported or 3rd party cookies and data may be disabled.", a: null}

UPDATE 2: Strangely enough, I disabled cookies, tried and it failed.更新 2:奇怪的是,我禁用了 cookies,尝试过但失败了。 Re-allowed cookies (just as it was initially) and it works now.重新允许 cookies (就像它最初一样),它现在可以工作了。 I would suggest capturing this error and prompting users to ensure they check their browser settings.我建议捕获此错误并提示用户确保他们检查浏览器设置。 I'm going to do this in my app.我将在我的应用程序中执行此操作。

暂无
暂无

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

相关问题 "firebase.auth().onAuthStateChanged 不工作" - firebase.auth().onAuthStateChanged Not Working 在 React 中放置 firebase.auth().onAuthStateChanged 的位置 - where to place firebase.auth().onAuthStateChanged in React firebase.auth().onAuthStateChanged 无限循环 - firebase.auth().onAuthStateChanged looping infinitely 类型错误:调用“firebase.auth().onAuthStateChanged((user)”时无法读取未定义的属性“auth” - TypeError: Cannot read property 'auth' of undefined when calling " firebase.auth().onAuthStateChanged((user)" Firebase 和 React TypeError: firebase.auth(...).onAuthStateChanged 不是 function - Firebase and React TypeError: firebase.auth(...).onAuthStateChanged is not a function 调用firebase.auth()。onAuthStateChanged()时出现“ Uncaught TypeError:无法读取未定义的属性'resolve'” - “Uncaught TypeError: Cannot read property 'resolve' of undefined” when calling firebase.auth().onAuthStateChanged() 刷新页面时,有时firebase.auth()。onAuthStateChanged返回null的用户 - Sometimes firebase.auth().onAuthStateChanged returns user of null when I refresh the page firebase.auth()。currentUser和onAuthStateChanged始终为null,尽管已登录 - firebase.auth().currentUser and onAuthStateChanged always null inspite of being signed in Javascript - firebase.auth().onAuthStateChanged 在登录/注销时未触发 - Javascript - firebase.auth().onAuthStateChanged not firing upon login/logout firebase.auth().onAuthStateChanged 登录后未执行? - firebase.auth().onAuthStateChanged not executed after sign-in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM