我有一个 Angular 2 应用程序,它使用 firebase 进行身份验证。
我想使用 Google 作为我的身份验证提供商,并进行所有设置以使其正常工作。
如果我尝试使用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] 信息:当前域未获得 OAuth 授权
运营。这将阻止signInWithPopup、signInWithRedirect、
linkWithPopup 和 linkWithRedirect 停止工作。添加您的域名
(localhost) 到 Firebase 控制台中的 OAuth 重定向域列表
-> 身份验证部分 -> 登录方法选项卡。
但该域列在我的 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);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)