If i copy the URL of welcome page after login and paste the URL of welcome page after logout it is redirect to login page it is working fine as i am checking with local storage name. Now the problem is I am not able to go to register page as it should be opened while user logged out for new user registration because the user will be null in local storage, Please anyone help me how to solve this problem.
This is the route for my application
app.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'app/components/login/login.html',
controller : 'loginCtrl'
}).when('/register/', {
templateUrl : 'app/components/register/register.html',
controller : 'registerController'
}).when('/welcome/', {
templateUrl : 'app/components/dashBoard/dashboard.html',
controller : 'welcomeController'
}).when('/logout', {
templateUrl : 'app/components/login/login.html',
controller : 'LogoutController'
}).when('/forgotPwd', {
templateUrl : 'app/components/forgotPassword/forgotPassword.html',
controller : 'forgotPwdController'
}).when('/changePwd', {
templateUrl : 'app/components/changePwd/changePassword.html',
controller : 'changePwdController'
}).otherwise({
redirectTo : "/"
});
} ]).run(function($rootScope, $location) {
$rootScope.$on( "$routeChangeStart", function(next) {
$rootScope.username = localStorage.getItem("UserName");
//alert("redirecting to login");
if ($rootScope.username === null) {
// no logged user, redirect to /login
if ( next.templateUrl === "app/components/login/login.html") {
} else {
$location.path("/");
}
}
});
});
next.templateUrlagainst more than one template, but the more pages you have that require anonymous access, the more complex that logic will become. You should consider rethinking this method of security.next.templateUrl; that introduces a case where changing your template file name breaks your authentication (very difficult to troubleshoot). If you must use$routeChangeStart, you might try adding a new boolean property in the route, likeallowAnonymousor something similar. However, I would still avoid this pattern if possible.