【问题标题】:Firebase Angular OAuth with Google email scope, auth.$authWithOAuthPopup vs ref.authWithOAuthPopupFirebase Angular OAuth 与 Google 电子邮件范围,auth.$authWithOAuthPopup 与 ref.authWithOAuthPopup
【发布时间】:2015-02-18 18:01:16
【问题描述】:

我已成功使用 Firebase 的 angular 库针对 Facebook 和 Google 对用户进行身份验证,但在使用 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'
    });
  };

这将弹出google auth窗口并成功登录。但是,在访问权限窗口中,它不会请求“电子邮件”范围访问权限。

如果我使用 ref.authWinOAuthPopup(...) 而不是 auth.$authWithOAithPopup(...) 它会正确请求电子邮件权限,并在身份验证后提供该信息。

我在这里做错了吗?或者,它是我应该报告的 Angularfire 错误吗?

Angularfire v0.9.2.

【问题讨论】:

    标签: angularjs oauth angularfire


    【解决方案1】:

    进一步挖掘后,我发现 $firebaseAuth.$authWithOAuthPopup 只需要两个参数(提供程序和选项),而 Firebase.authWithOAuthPopup 需要三个参数(提供程序、onComplete 回调、选项)。因此,使用重构来使用返回的 Promise 而不是传入回调函数解决了这个问题。

    【讨论】:

      【解决方案2】:

      为了更清楚,我想添加我的 sn-ps 来展示如何将电子邮件范围传递给 google oAuth。

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

      请看下面的sn-p:

      $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());
       }]);
      

      【讨论】:

        【解决方案3】:

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

        .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'});
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-09-26
          • 1970-01-01
          • 2016-10-06
          • 2016-07-04
          • 1970-01-01
          • 2018-12-15
          • 1970-01-01
          相关资源
          最近更新 更多