Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.factory('authInterceptor',['$q','$window','$location', function ($q, $window, $location) {
- return {
- request: function (config) {
- config.headers = config.headers || {};
- if ($window.localStorage.sessionToken) {
- config.headers.Authorization = 'Bearer ' + $window.localStorage.sessionToken;
- }
- return config;
- },
- response: function (response) {
- if (response.status === 401) {
- $location.path('/auth/login');
- console.log('Please login before to access the page');
- }
- return response || $q.when(response);
- }
- };
- }]);
- app.config(['$routeProvider','$locationProvider','$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
- $routeProvider
- .when('/',{
- templateUrl:'views/home/index.html',
- controller:'Home'
- })
- .when('/auth/login',{
- templateUrl:'views/auth/login.html',
- controller:'AuthLogin'
- })
- .when('/auth/signup',{
- templateUrl:'views/auth/signup.html',
- controller:'AuthSignup'
- })
- .otherwise({
- redirectTo:'/'
- });
- $locationProvider.html5Mode(true);
- $httpProvider.defaults.withCredentials = true;/*XHR Credentials*/
- $httpProvider.interceptors.push('authInterceptor');/*Session interceptor*/
- }]);
- console.log(config.headers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement