Javascript 使用augularfire OAuth登录facebook的策略

Javascript 使用augularfire OAuth登录facebook的策略,javascript,angularjs,firebase,ionic,angularfire,Javascript,Angularjs,Firebase,Ionic,Angularfire,我在loginCtrl控制器和login.html上使用下面的代码。它起作用了。但我无法正确设置工作流程。我想做这样的事情 //check if the current $scope has authData //if so redirect to (or render) home.html //if not stay in the login page. 我有一个新的角度,并试图找出。在使用express的nodejs上,我似乎很清楚,当接收到的数据良好时,我会进行jsut渲染或重定向。混

我在loginCtrl控制器和login.html上使用下面的代码。它起作用了。但我无法正确设置工作流程。我想做这样的事情

//check if the current $scope has authData
 //if so redirect to (or render) home.html
//if not stay in the login page.
我有一个新的角度,并试图找出。在使用express的nodejs上,我似乎很清楚,当接收到的数据良好时,我会进行jsut渲染或重定向。混淆了如何处理角度

javascript

.controller('loginCtrl', function ($scope, Auth, Login) {


  $scope.login = function() {
    Auth.$authWithOAuthPopup("facebook")
        .then(function(authData){
          console.log(authData);
        })
        .catch(function(err){
          if(err) {
           console.log("Authentication failed!!", err);
          }
        })
  };
})

.factory('Auth', function($firebaseAuth){
  var usersRef = new Firebase("https://dazzling-heat-4971.firebaseio.com/users");
  return $firebaseAuth(usersRef);
})
html


使用Facebook登录

AngularFire内置支持简化路由和身份验证

您可以插入当前已验证的用户,而不是在控制器中检查已验证的用户。

这意味着您不必在控制器或视图中执行任何
if-else
检查,这将简化代码

您可以通过路由器中的
resolve()
函数注入用户。Ionic使用
ui路由器
。用户登录后,您需要使用路由名称调用
$state.$go()
。这将路由到视图并调用控制器

下面的代码样式来自角度样式指南。这只是编写角度代码的另一种方式,所以不要让它太分散你的注意力

angular.module('app', ['firebase'])
   .run(ApplicationRunSettings)
   .config(ApplicationConfig)
   .controller('loginCtrl', LoginCtrl);

// The authData param is injected from the router
function UserProfileCtrl($scope, authData) {
  $scope.user = authData;
}

function LoginCtrl($scope, Auth) {
   Auth.$authWithOAuthPopup("facebook")
    .then(function(authData){
      $state.go('userProfile');
    })
    .catch(function(err){
      if(err) {
       console.log("Authentication failed!!", err);
      }
    })
}

// Set up the router to resolve authenticated users
function ApplicationConfig($stateConfig) {
   $stateProvider
     .state("home", {
       controller: "UserProfileCtrl",
       templateUrl: "views/userProfile.html",
       resolve: {
        // controller will not be loaded until $waitForAuth resolves
        "currentAuth": ["Auth", function(Auth) {
        // $waitForAuth returns a promise so the resolve waits for it to complete
           return Auth.$waitForAuth();
        }]
      }
   }
}

// When there's an error, go to the home view
function ApplicationRunSettings($rootScope, $state) {
  $rootScope.$on("$stateChangeError", function(event) {
  // We can catch the error thrown when the $requireAuth promise is rejected
  // and redirect the user back to the home page
  if (error === "AUTH_REQUIRED") {
    $state.go("home");
  }
}
更多信息,请查看路由器验证

angular.module('app', ['firebase'])
   .run(ApplicationRunSettings)
   .config(ApplicationConfig)
   .controller('loginCtrl', LoginCtrl);

// The authData param is injected from the router
function UserProfileCtrl($scope, authData) {
  $scope.user = authData;
}

function LoginCtrl($scope, Auth) {
   Auth.$authWithOAuthPopup("facebook")
    .then(function(authData){
      $state.go('userProfile');
    })
    .catch(function(err){
      if(err) {
       console.log("Authentication failed!!", err);
      }
    })
}

// Set up the router to resolve authenticated users
function ApplicationConfig($stateConfig) {
   $stateProvider
     .state("home", {
       controller: "UserProfileCtrl",
       templateUrl: "views/userProfile.html",
       resolve: {
        // controller will not be loaded until $waitForAuth resolves
        "currentAuth": ["Auth", function(Auth) {
        // $waitForAuth returns a promise so the resolve waits for it to complete
           return Auth.$waitForAuth();
        }]
      }
   }
}

// When there's an error, go to the home view
function ApplicationRunSettings($rootScope, $state) {
  $rootScope.$on("$stateChangeError", function(event) {
  // We can catch the error thrown when the $requireAuth promise is rejected
  // and redirect the user back to the home page
  if (error === "AUTH_REQUIRED") {
    $state.go("home");
  }
}