简体   繁体   中英

how to restrict logged user to move on login page or signup page in angular js?

i have following code :

.run(function($rootScope, $location, Auth) {

    //Redirect to login if route requires auth and you're not logged in
    $rootScope.$on('$routeChangeStart', function(event, next) {

       Auth.isLoggedInAsync(function(loggedIn) {
        if (!loggedIn) {//when user is not logged in
          if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login");

          else{
            $location.path('/homePage');
          }
        }
        else if(loggedIn){ //when user loged in
          if ($location.path() == "/participant/signup" || $location.path() == "/researcher/signup" || $location.path() == "/participant/login" || $location.path() == "/researcher/login" || $location.path() == "/admin/login"){
            event.preventDefault();
            //event.stopPropagation();
          }
        }
      });
    });
  })

i want to restrict logged user to move on login or signup page. the above code is not working please suggest me where i am doing wrong. if you have a good way to handle this please suggest?

Define an interceptor to handle user status

angular.module('App').factory('authInterceptor', ['$location', 'Auth', function ($location, Auth) {

        var authInterceptorService = {};
        if (!Auth.isLoggedIn)
            $location.url('/login');

        return authInterceptorService;
    }]);

In App.js

angular.module('App').config(function ($httpProvider) {
    $httpProvider.interceptors.push('authInterceptorService');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM