2014-12-02 62 views
0

我需要一定的路線來要求認證的路線,我使用這個模塊:認證的角度

https://github.com/enginous/angular-oauth

其中有$ scope.authenticate但我試圖找出如何訪問$範圍/從$ routeProvider函數。我看到一個例子調用工廠函數,但這不完全是我想要做的。

'use strict'; 

angular.module('app', [ 
    'ngRoute', 
    'angularOauth', 
    'googleOauth', 
    'app.global', 
    'app.home', 
    'app.view' 
]). 
config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/', { 
    templateUrl: 'views/home/index.html', 
    controller: 'home' 
    }); 
    $routeProvider.when('/view', { 
    templateUrl: 'views/view/index.html', 
    controller: 'view', 
    resolve: { 
     factory: checkAuth 
    } 
    }).otherwise({redirectTo: '/'}); 
    $routeProvider.otherwise({redirectTo: '/'}); 
}]); 

var checkAuth = function() { 
}; 

回答

0

解決方案的註釋是在Angular的其他地方使用的相同的依賴注入註釋。您的驗證應該生活在可測性和可重用性服務,所以注入和調用服務的方法的邏輯恰恰是你應該做的事情:

resolve: { 
    auth: ['AuthenticationService', function (AuthenticationService) { 
    // Authentication service injected, call your method 
    return AuthenticationService.isAuthenticated(); 
    }] 
} 

var checkAuth = ['AuthenticationService', function (AuthenticationService) { 
    // Authentication service injected, call your method 
    return AuthenticationService.isAuthenticated(); 
}]; 

angular.module('app', [ 
    // ... 
]). 
config(['$routeProvider', function($routeProvider) { 
    // ... 
    $routeProvider.when('/view', { 
    templateUrl: 'views/view/index.html', 
    controller: 'view', 
    resolve: { 
     auth: checkAuth 
    } 
    }) 
    // ... 
}]);