Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. app.factory('authInterceptor',['$q','$window','$location', function ($q, $window, $location) {
  2. return {
  3. request: function (config) {
  4. config.headers = config.headers || {};
  5. if ($window.localStorage.sessionToken) {
  6. config.headers.Authorization = 'Bearer ' + $window.localStorage.sessionToken;
  7. }
  8. return config;
  9. },
  10. response: function (response) {
  11. if (response.status === 401) {
  12.  
  13. $location.path('/auth/login');
  14. console.log('Please login before to access the page');
  15. }
  16. return response || $q.when(response);
  17. }
  18. };
  19. }]);
  20.  
  21. app.config(['$routeProvider','$locationProvider','$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
  22.  
  23. $routeProvider
  24. .when('/',{
  25. templateUrl:'views/home/index.html',
  26. controller:'Home'
  27. })
  28. .when('/auth/login',{
  29. templateUrl:'views/auth/login.html',
  30. controller:'AuthLogin'
  31. })
  32. .when('/auth/signup',{
  33. templateUrl:'views/auth/signup.html',
  34. controller:'AuthSignup'
  35. })
  36. .otherwise({
  37. redirectTo:'/'
  38. });
  39.  
  40. $locationProvider.html5Mode(true);
  41.  
  42. $httpProvider.defaults.withCredentials = true;/*XHR Credentials*/
  43. $httpProvider.interceptors.push('authInterceptor');/*Session interceptor*/
  44.  
  45. }]);
  46.  
  47. console.log(config.headers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement