4

解决我试图使用新的火力地堡与验证系统,并通过resolves限制在我$routeProvider路线。AngularJS路线,并与新的火力地堡验证

但是,我不是很理解。

这就是我所拥有的。在我的.config函数中,我定义了路线并初始化了Firebase。以下是我的配置块。

$routeProvider 
    .when('/', { 
     templateUrl: 'templates/dashboard.ejs', 
     //resolve. this needs restricted 
    }) 
    .when('/login', { 
     templateUrl: 'templates/login.ejs' 
    }) 

firebase.initializeApp(config); 

我已经被我.run()块上市角新文档的网站,这些功能都可以找到。现在

.run(function($rootScope, $location) { 

    $rootScope.authentication = firebase.auth(); 

    /*firebase.auth().onAuthStateChanged(function(user) { 
     if(user) { 
      $rootScope.user = user; 
     } else { 
      $rootScope.user = false; 
      $location.path('/login') 
     } 
    })*/ 

    var user = firebase.auth().currentUser; 

    if (user) { 
     $rootScope.user = user; 
    } else { 
     $rootScope.user = false; 
     console.log('No User!'); 
     $location.path('/login'); 
    } 
}) 

,上面我所仅发射every other time我访问我的网站上的任何URL。

所以我的问题是,我该如何采取什么在我的.RUN()函数,并使其成为我routeProvider一个决心,所以我能够再次限制路线。

随着老火力,你只需调用$ firebaseAuth()之类的下方,有一个被称为类似下面的routeChangeError功能。

// In config block. **old way** 
$routeProvider 
     .when('/', { 
      templateUrl: 'templates/dashboard.ejs', 
      resolve: { 
       "currentAuth": ["$firebaseAuth", function($firebaseAuth) { 
        var ref = new Firebase(fbUrl); 
        var authObj = $firebaseAuth(ref); 

        return authObj.$requireAuth(); 
       }] 
      } 
     }) 

然后是.run()块中的routechangeerror。

// .run block **old way** 
.run(function($rootScope, $location) { 
    $rootScope.$on("$routeChangeError", function(event, current, previous, eventObj) { 
     if (eventObj === 'AUTH_REQUIRED') { 
      console.log('auth required!'); 
      $location.path("/login"); 
     } 
    }); 
}) 

这不起作用了,因为您不再使用$firebaseAuth()了。或者new Firebase(fbUrl);

UPDATE

我觉得这是一个有点混乱,但寻找到的路线多一点,我想出了这个。

在我的路线的决心:

.when('/', { 
      templateUrl: 'templates/dashboard.ejs', 
      resolve: { 
       userAuthenticated: ["$http", "$q", function($http, $q) { 
        var deferred = $q; 
        if(firebase.auth().currentUser) { 
         deferred.resolve(); 
        } else { 
         deferred.reject(); 
         console.log('Really!?'); 
        } 
        return deferred.promise; 
       }] 
      } 
     }) 

然后简单routeChangeError。

$rootScope.$on("$routeChangeError", function (event, current, previous, rejection) { 
     alert("Not authorised"); 
}) 

唯一的问题是,它显示deferred.promise返回undefined。它没有激活routeChangeError函数。即使我的console.log()被调用。

难道这是因为当没有用户认证firebase.auth().currentUser返回空?

回答

4

该方法已更名为$waitForSignIn()$requireSignIn

您也可以使用$firebaseRefProvider服务来配置您的数据库URL,以便它自动配置$firebaseAuthService

angular.module('app', ['firebase']) 
    .constant('FirebaseDatabaseUrl', '<my-firebase-url>') 
    .controller('HomeCtrl', HomeController) 
    .config(function($firebaseRefProvider, FirebaseDatabaseUrl, $routeProvider) { 
    $firebaseRefProvider.registerUrl(FirebaseDatabaseUrl); 
    $routeProvider.when("/home", { 
     controller: "HomeCtrl", 
     templateUrl: "views/home.html", 
     resolve: { 
      // controller will not be loaded until $waitForSignIn resolves 
      "firebaseUser": function($firebaseAuthService) { 
      return $firebaseAuthService.$waitForSignIn(); 
      } 
     } 
     }).when("/account", { 
     controller: "AccountCtrl", 
     templateUrl: "views/account.html", 
     resolve: { 
      // controller will not be loaded until $requireSignIn resolves 
      "firebaseUser": function(Auth) { 
      // If the promise is rejected, it will throw a $stateChangeError 
      return $firebaseAuthService.$requireSignIn(); 
      } 
     } 
     }); 
    }); 

function HomeController($scope, $firebaseRef, firebaseUser) { 

} 
+0

我不知道您使用的是什么版本或第三方依赖项,但这对我来说完全不起作用,也不像我在文档中找到的任何东西。当我尝试将firebase模块加载到我的应用程序中时,出现一个未定义的错误。那么,你在使用什么第三方? –

+0

您也不会像这样定义您的网址,您可以使用Firebase提供的代码块初始化Firebase。我的问题不是关于第三方依赖关系。 –

+0

我在Firebase团队工作,我是AngularFire的维护者。我写了'$ firebaseRefProvider'和'$ firebaseAuthService'功能。这是一种使用数据库引用处理依赖注入的方法。我们正在为它的文档工作。查看github上的文档以获取身份验证方法:https://github.com/firebase/angularfire/blob/master/docs/guide/user-auth.md。 –

0

所以我不知道这是否是地球上最干净的切割代码,但它的工作原理。

火力地堡现在提供了获取,如果有一个当前用户身份验证的新功能。初始化Firebase后,您可以访问此功能。 firebase.auth().currentUser

所以,在你的路线,你可以创建和解决基于此函数的结果的承诺(如果登录和零如果没有这个函数返回用户详细信息)。要确保定义为递延$q.defer();(我这个挣扎,你可以在我的问题的更新见)

$routeProvider 
    .when('/', { 
      templateUrl: 'templates/dashboard.ejs', 
      resolve: { 
       userAuthenticated: ["$http", "$q", function($http, $q) { 
        var deferred = $q.defer(); 
        if(firebase.auth().currentUser) { 
         deferred.resolve(); 
        } else { 
         deferred.reject('NOT_AUTHORIZED'); 
        } 
        return deferred.promise; 
       }] 
      } 
     }) 

然后在对角的运行功能,可以观赏路线的错误变动。

.run(function($rootScope, $location) { 
    $rootScope.$on("$routeChangeError", function (event, current, previous, rejection) { 
     if(rejection == 'NOT_AUTHORIZED') 
     { 
     $location.path('/login'); 
     } 
    }) 
}) 

在上面的函数中,您只需观察路径变化中的错误。如果拒绝等于'NOT_AUTHORIZED'(在解析函数中我们的拒绝函数中传递),那么我们将用户重定向到登录屏幕以进行身份​​验证。

注意:在这个routeChangeError函数中,你可以自由地做你喜欢的事情,甚至可以显示一个小小的咆哮,该用户没有被授权。