1

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("/");
        }
      }
    });
});
5
  • this pattern is very fragile and error prone. You can solve the problem you are encountering now by checking next.templateUrl against 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. Commented Dec 30, 2015 at 7:09
  • Hi @Claies any idea for this Commented Dec 30, 2015 at 7:11
  • There are plenty of examples of good ways to implement this logic; First, don't check against 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, like allowAnonymous or something similar. However, I would still avoid this pattern if possible. Commented Dec 30, 2015 at 7:18
  • @Sudhir, So, do you want to go to register page when user enter the url for the register page in a new tab or window? Commented Dec 30, 2015 at 7:20
  • @AbhilashPA no after logout i will be redirected to login page and the localstorage will be cleared, Now i want to open a register page in same window link is provided in login page itself. Commented Dec 30, 2015 at 7:24

1 Answer 1

1
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("/");
        }
      }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

this is indeed the solution I was suggesting, but I highly recommend avoiding this, for the reasons I stated in my comments above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.