Firebase Angular OAuth与谷歌电子邮件范围,auth$authWithOAuthPopup与ref.



我成功地使用Firebase的angular库对用户进行了针对Facebook和谷歌的身份验证,但在使用firebaseAuth的$authWithOAuthPopup时,检索用户的电子邮件时遇到了麻烦。

这是我的登录功能。

var ref = new Firebase(FIREBASE_URL);
var auth = $firebaseAuth(ref);      
loginGoogle: function () {
    console.log('Logging in Google.');
    return auth.$authWithOAuthPopup('google', function(error, user){
      //TODO: Handle Failed login better
      console.log('Google login failed');
      console.log(error);
    },{
      scope: 'email'
    });
  };

这将弹出谷歌身份验证窗口并成功登录。但是,在访问权限窗口中,它不会请求"电子邮件"范围的访问。

如果我使用ref.authWinOAuthPopup(…)而不是auth$authWithOAithPopup(…)它确实正确地请求电子邮件排列,并在auth之后传递该信息。

我是不是做错了什么?或者,我应该报告的是Angularfire病毒吗?

Angularfire v0.9.2。

在进一步挖掘后,我发现$firebaseAuth$authWithOAuthPopup只接受两个参数(provider和options),而Firebase.authWithOAuthPopup接受三个参数(provider、onComplete回调、options)。因此,使用重构来使用返回的promise,而不是传入回调函数,解决了这个问题。

为了更清楚,我想添加我的片段来展示如何将电子邮件范围传递给googleoAuth。

正如您所提到的,如果您使用promise而不是回调来进行身份验证,那么您可以将电子邮件范围的选项传递给auth。调用作为第二个参数,然后电子邮件地址将在有效负载中可用。

请看下面的片段:

$scope.loginGoogle = function() {
    Auth.$authWithOAuthPopup("google", {scope: ['email']}).then(function(authData) {
        // handle authData in onAuth handler in app.js in run method with Auth.$onAuth(function(authData) { ... });
        console.log("Authenticated successfully with payload:", authData);
        $location.path(REDIRECT_ROUTE);
    })
    .catch(function(err) {
        $scope.err = errMessage(err);
    });
};
function errMessage(err) {
    var msg = "";
  //return angular.isObject(err) && err.code? err.code : err + '';
  switch (err.code) {
      case "INVALID_EMAIL":
      case "INVALID_PASSWORD":
      case "INVALID_USER":
        console.log("The specified user account email is invalid.");
            msg = "E-mail or password is invalid.";
        break;
    /*
      case "INVALID_PASSWORD":
        console.log("The specified user account password is incorrect.");
        break;
      case "INVALID_USER":
        console.log("The specified user account does not exist.");
        break;
      default:
        // password or email empty;
        console.log("Error logging user in:", err);*/
    };
    return msg;
}
angular.module('firebase.auth', ['firebase', 'firebase.utils'])
.factory('Auth', ['$firebaseAuth', 'fbutil', function($firebaseAuth, fbutil) {
    return $firebaseAuth(fbutil.ref());
 }]);

这就是我所做的,它对我的有效

.config(function ($firebaseRefProvider) { $firebaseRefProvider.registerUrl('https://****.firebaseio.com/'); })

然后在控制器中

loginWithGoogle: function(){
    var ref = $firebaseRef.default;
    ref.authWithOAuthPopup("google", function(error, authData) {
      if (error) {
        ...
      } else {
        console.log(authData.google.email);
      }
    },{scope: 'email'});
  }

最新更新