【问题标题】:Firebase (angular) facebook authWithRedirect not working correctingFirebase(角度)facebook authWithRedirect 无法正常工作
【发布时间】:2015-07-23 16:44:48
【问题描述】:

我使用 angularfire(Ionic 应用程序)通过 facebook 按钮登录,并且正在使用 $authWithOAuthRedirect 重定向到 facebook 身份验证页面,然后返回到应用程序。

如果我在调用 facebook 重定向身份验证的函数内部有一个 $onAuth 方法,它可以工作,但如果它在该函数之外,它就不起作用。我还有一个使用电子邮件和密码进行身份验证的表单,如果 $onAuth 在 facebook 身份验证方法中,那么电子邮件和密码登录将无法正常工作。

这是使用初始化 firebaseAuth 的 Auth 服务的控制器:

/****
 * Login Page Controller
 * Uses angularfires OAuth for facebook login
****/ 

angular.module('starter')
//inject the Auth service from loginService.js
.controller('LoginController', ['$scope', '$state', 'Auth', function($scope, $state, Auth) {

  //userlogin scope variables: error, authData
  $scope.userLogin = {
    error: null
  };


  //Login for email password
  $scope.loginUser = function() {
    Auth.$authWithPassword({
      email: $scope.userLogin.email,
      password: $scope.userLogin.password
    })
    .then(
      function(authData){
        $scope.userLogin.authData = authData;
      }, 
      function(error) {

        if(error.code == 'INVALID_EMAIL') {
          $scope.userLogin.error = "Invalid Email";
        }  
        else if(error.code == 'INVALID_PASSWORD'){
          $scope.userLogin.error = "Email or Password is incorrect";
        } else {
          $scope.userLogin.error = "Enter a valid email and password";
        }

      }
    )
  };

  //login function for facebook login 
  $scope.loginUserFacebook = function() {
    Auth.$authWithOAuthRedirect('facebook')
    .then(function(authData) {
      console.log('hi');
    })
    //if error due to no redirects 
    .catch(function(error) {

      //authenticate with popup for emulators
      if (error.code === 'TRANSPORT_UNAVAILABLE') {
        Auth.$authWithOAuthPopup('facebook').then(function(authData) {
          // console.log(authData);
        });
      } else {
        console.log(error);
      }

    });
  };
    //After successful auth
    Auth.$onAuth(function(authData) {
      if (authData === null) {
        console.log('Not logged in');
      } 
      else {
         $state.go('driver');
         console.log('Logged in as ' + authData.uid);
      }

      //set authData on controller scope
      $scope.userLogin.authData = authData;
    });

}]);

因此,如果我将 Auth.$onAuth 上移到 loginUserFacebook 函数中,那么它可以工作,但会将 loginUser 的电子邮件和密码弄乱。

我猜 facebook 重定向并没有像我想的那样解决承诺,但我已经广泛阅读了 Firebase 文档,但无法弄清楚。

【问题讨论】:

  • 如果下面的答案没有为您澄清/解决,请随时在 firebase dot com 上 ping 我。

标签: angularjs firebase ionic facebook-authentication


【解决方案1】:

免责声明:我在 Firebase 工作

Auth.$authWithOAuthRedirect(...) 返回的承诺有点误导,因为这个承诺永远不会被解决——它只会失败。如果您尝试在不支持浏览器重定向的平台(例如PhoneGap)上使用浏览器重定向,我们将抛出TRANSPORT_UNAVAILABLE 错误,并且promise 将被拒绝。在所有其他“成功”的情况下,浏览器将重定向到 OAuth 提供程序,然后返回到您的应用程序,但之前的承诺状态不再可用。

为了在浏览器重定向成功后获取您的身份验证会话,只需使用Auth.$onAuth(function(authData){...}) 来监控身份验证状态。在 OAuth 提供者第一次返回时,后台的 Firebase 客户端将完成会话的创建,并使用用户的当前身份验证状态向传递给 onAuth(function(authData){...}) 的回调引发一个事件。

详细了解 Firebase 身份验证方法 here 和 AngularFire 的身份验证工具 here

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 2017-10-20
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多