Reputation: 579
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("/");
}
}
});
});
Upvotes: 1
Views: 583
Reputation: 4208
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 ( $location.$$url == "/" || $location.$$url == "/register/") {
// do nothing. Do not redirect
} else {
// redirect to default path
$location.path("/");
}
}
});
Upvotes: 1