AngularJS - Redirect to Login page and Persistence of Session ID

后端 未结 4 1359
一向
一向 2021-01-30 14:57

I am looking for a way to do these two things, first I want to redirect the user to a login page if no SessionID is found and second I would like to hear your opinion about pers

4条回答
  •  被撕碎了的回忆
    2021-01-30 15:48

    I use a similar strategy (intercepting 401 responses from the server). You can check out the full example here : https://github.com/Khelldar/Angular-Express-Train-Seed

    It uses node and mobgodb on the backend for session store and a trimmed down http interceptor on the client that doens't retry requests like the one Dan linked above:

     var interceptor = ['$q', '$location', '$rootScope', function ($q, $location, $rootScope) {
            function success(response) {
                return response;
            }
    
            function error(response) {
                var status = response.status;
                if (status == 401) {
                    $location.path('/login');
                }
                return $q.reject(response);
            }
    
            return function (promise) {
                return promise.then(success, error);
            }
        }];
        $httpProvider.responseInterceptors.push(interceptor);
    

提交回复
热议问题