Reputation: 5101
I am developing a nodejs
application using angular
. here I just confused with following scenarios. i require the best practice to know to handle all this:
In route
All this scenario is to handled in fronend or backend? else what this the correct way?
here is samples:
Never Required to test the authentication:
.when('/login', {
templateUrl : 'app/views/pages/login.html',
controller : 'mainController',
controllerAs: 'login'
})
.when('/login', {
templateUrl : 'app/views/pages/createUser.html',
controller : 'userController',
controllerAs: 'user'
})
Always required Authentication :
.when('/login', {
templateUrl : 'app/views/pages/showProduct.html',
controller : 'productController',
controllerAs: 'product'
})
.when('/login', {
templateUrl : 'app/views/pages/showStatics.html',
controller : 'staticController',
controllerAs: 'static'
})
before authentication with wrong URL:
.when('/login', {
templateUrl : 'app/views/pages/testingWrong'
})
After authentication with wrong URL :
.when('/static', {
templateUrl : 'app/views/pages/showStatics.html',
controller : 'staticController',
controllerAs: 'static'
})
.when('/static/wrong', {
templateUrl : 'app/views/pages/showStatics.html',
})
If there is a tutorial, please share me. or if there is a solution please post with details to understand.
Thanks in Advance.
Upvotes: 0
Views: 44
Reputation: 46
You need add resolve function param, just like below
when('products', {
templateUrl : 'app/views/pages/showProduct.html',
controller : 'productController',
controllerAs: 'product',
resolve: function($q, $location, Auth) {
var deferred = $q.defer();
deferred.resolve();
if (!Auth.isLoggedIn()) {
$location.path('/login');
}
return deferred.promise;
}
});
Auth will be some service which keeps user's logged status. Hope this will help you.
Upvotes: 2