簡體   English   中英

如何限制登錄用戶在angular js的登錄頁面或注冊頁面中移動?

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

我有以下代碼:

.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();
          }
        }
      });
    });
  })

我想限制登錄用戶在登錄或注冊頁面上移動。 上面的代碼不起作用,請建議我在哪里做錯了。 如果您有解決此問題的好方法,請提出建議?

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;
    }]);

在App.js中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM