2013-07-06 81 views
3

我試圖在我的應用程序中對大多數路由的用戶進行身份驗證。AngularJS用解析驗證多個路由

有沒有辦法在全球所有路線上做到這一點?所以我不需要有以下內容:

resolve : { 
    //This function is injected with the AuthService where you'll put your authentication logic 
    'auth' : function(AuthService){ 
     return AuthService.authenticate(); 
    } 
} 

對每個$routeProvider.when()調用。

+1

這[HTTP驗證攔截模塊(https://github.com/witoldsz/angular-http-auth)可以,如果它不幫偶完全適合你的需要。 – Gloopy

+0

謝謝。這實際上是一個非常有用的方法...... – AndrewMcLagan

回答

2

Gloopy的建議非常有趣,我可能會在未來實施類似的方法。

現在我已經採取了更簡單的方法:

gm.config(['$routeProvider', 'PathProvider', function($routeProvider, PathProvider) { 

    var authResolver = { // although this does work there could be a better way to do this. 
     'auth' : function(AuthenticationService) { 
      return AuthenticationService.isLoggedIn(); 
     } 
    }; 

    $routeProvider.when('/authenticatedRoute', { 
     templateUrl: PathProvider.view('application/dashboard/index.html'), 
     controller: 'dashboardController', 
     resolve: authResolver 
    }); 

    $routeProvider.otherwise({ 
     redirectTo: '/dashboard', 
     resolve: authResolver 
    }); 

}]);