I have an application with a working system of authentication. My issue here is that when a user is connected, he can still access by URL to the login page. I would like to redirect all connected users to home page instead of login page.
So, when someone ask for /authentication/login
:
- If the user is connected, he will be redirected to
/home
. - If nobody is connected, access to
authentication/login
is open.
Here is my actual working code (it doesn't redirect a connected user to the home page).
angular.module('authentication').config(RouteConfig);
RouteConfig.$inject = ['$routeProvider', 'UserSession'];
function RouteConfig($routeProvider, UserSession) {
$routeProvider.when('/authentication/login/', {
templateUrl: 'section/authentication/login.tmpl',
controller: 'LoginController',
controllerAs: 'lo'
});
}
Is that possible to add a conditional statement in the above code? Something like:
$routeProvider.when('/authentication/login/', {
if(UserSession.getUser() != null) {
// Go to Home page instead
} else {
// Normal way
}
});