Firebase 和 angularFire - 从 Google 身份验证获取电子邮件适用于 $authWithOAuthPopup 但无法使用 $authWithOAuthRedirect

标签 firebase angularfire

以下代码片段有效,除非用户的浏览器配置(例如带有 Chrome 的 iOS)将它发送到 $authWithOAuthRedirect block - 然后它会失败。

失败是指 $authWithOAuthRedirect 方法有效并且用户可以批准身份验证,但它无法将范围正确发送给 Google,并且未请求电子邮件访问权限。

var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);

auth.$authWithOAuthPopup(provider, scope).then(function (authData, error) {
    if (error && error.code === "TRANSPORT UNAVAILABLE") {
         auth.$authWithOAuthRedirect(provider, function(error) {}, scope);
    }
});

简化后,此代码将无法请求用户的电子邮件:

var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthRedirect(provider, function(error) {}, scope);

感谢您的帮助!

最佳答案

我认为问题在于您使用的是非角度版本的语法:Firebase.authWithOAuthRedirect(provider[, callback, scope])

你应该使用 AngularFire 版本: $firebaseAuth.$authWithOAuthRedirect(提供者[, 选项])

此版本返回一个 promise ,因此您的简化代码应如下所示:

var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthRedirect(provider, scope).then(function (authObject) {
    // Handle success
}, function (error) {
    // Handle error
});

关于Firebase 和 angularFire - 从 Google 身份验证获取电子邮件适用于 $authWithOAuthPopup 但无法使用 $authWithOAuthRedirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30048507/

相关文章:

angularjs - 如何从 AngularJS 到 Firebase 添加、删除和编辑数组中的新对象

firebase - 如何在 AngularFire 中的嵌套 Child 中 orderByChild ?

java - Firestore whereEqualTo、orderBy 和 limit(1) 不起作用

ios - 应用程序在后台 iOS 时的 Firebase 数据库事务

python - 如何使用 python_jwt 验证 firebase token

javascript - Angularjs Firebase 获取数据列表

javascript - 使用 AngularFire 简化 Angular Controller 发布到 Firebase

ios - Firebase 通知适用于 android,不适用于 ios(ionic 3)

ios - admob 插页式广告永远不会准备好

javascript - Firebase createUser 方法返回未定义