列出了 Firebase 重定向域,但 signInWithRedirect 仍然出错

Firebase redirect domain listed but still gives an error with signInWithRedirect

我有一个使用 firebase 进行身份验证的 angular 2 应用程序。 我想使用 Google 作为我的 authenticatonprovider 并设置所有内容以便它应该工作。

如果我尝试使用 signinWithPopup 进行身份验证(如文档中所述),它会起作用:

firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
}).catch(function(error) {
  // Handle Errors here.
});

但是如果我尝试使用重定向的相同代码 firebase.auth().signInWithRedirect(provider) 我收到错误:

[firebase-auth] Info: The current domain is not authorized for OAuth operations. This will prevent signInWithPopup, signInWithRedirect, linkWithPopup and linkWithRedirect from working. Add your domain (localhost) to the OAuth redirect domains list in the Firebase console -> Auth section -> Sign in method tab.

但是该域在该部分的我的 firebase 控制台中列出(localhost 甚至是默认允许的域之一)。 错误指出这也会阻止仍然有效的 signinwithpopup。

有人知道为什么弹出方法有效而重定向方法无效吗?

所以我终于弄清楚问题出在哪里了。 问题不是基于域的,而是应用程序流程中的问题。 在 app.component.ts 的构造函数中调用了 loginwithpopup 代码(目的是让人们在网站加载后立即登录)。

我将该代码更改为 loginwithredirect 函数,但是因为它是在构造函数中调用的,所以每次您从 google 身份验证页面重定向回该站点时都会调用它(这使您陷入永恒循环). 我仍然不知道为什么域错误会出现在浏览器控制台中,但是当我修复流程以首先检查我们是否从重定向返回时,问题就消失了。

为了完整起见,我使用重定向方法登录的当前代码:

firebase.auth().getRedirectResult().then(function (result) {
  if (result.credential) {
    // This gives you a Google Access Token. You can use it to access the Google API.
    var token = result.credential.accessToken;
    // The signed-in user info.
    var user = result.user;
  } 
  else {
    var provider = new firebase.auth.GoogleAuthProvider();

    provider.addScope('https://www.googleapis.com/auth/gmail.readonly');
    provider.addScope('https://www.googleapis.com/auth/calendar');
    provider.addScope('https://www.googleapis.com/auth/drive');
    provider.addScope('https://www.googleapis.com/auth/drive.appdata');
    provider.addScope('https://www.googleapis.com/auth/drive.file');

    firebase.auth().signInWithRedirect(provider);

   }
}).catch(function (error) {
  // Handle Errors here.
  console.log(error);
});