AngularJS - Redirect to Login page and Persistence of Session ID

后端 未结 4 1212
一向
一向 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:44

    this will work. It works fine in my application

    var interceptor = function ($q, $location) {
            return {
                request: function (config) {//req
                    console.log(config);
                    return config;
                },
    
                response: function (result) {//res
                    console.log('Repos:');
                    console.log(result.status);
                    return result;
                },
    
                responseError: function (rejection) {//error
                    console.log('Failed with', rejection.status, 'status');
                    if (rejection.status == 403) {
                        $location.url('/dashboard');
                    }
    
                    return $q.reject(rejection);
                }
            }
        };
        module.config(function ($httpProvider) {
            $httpProvider.interceptors.push(interceptor);
        });
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-30 15:49

    I would start here, Witold has created this cool interceptor that works off of http responses. I use it and its been really helpful.

    0 讨论(0)
  • 2021-01-30 15:50

    In my case, I used

    1. interceptor with $httpProvider
    2. config
    3. and $window dependency, as $location just appended the path to the existing url. What happened was like "http://www.tnote.me/#/api/auth", and it should have bene like "http://www.tnote.me/auth"

    The code snippet is like this.

    noteApp = angular.module('noteApp', ['ngRoute', 'ngCookies'])
      .factory('authInterceptor', ['$rootScope', '$q', '$cookies', '$window', 
        function($rootScope, $q, $cookies, $window) {
          return {
            request: function (req) {
              req.headers = req.headers || {};
              if ($cookies.token) {
                req.headers.Authorization = 'Bearer ' + $cookies.token;  
              }
    
              return req;
            },
            responseError: function (rejection) {
              if (rejection.status == 401) {
                $window.location = '/auth';      
              }
    
              return $q.reject(rejection);
            }
          }
      }])
      .config(['$routeProvider', '$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');  
        }
      ])
    
    0 讨论(0)
提交回复
热议问题