Advertisement
Guest User

Untitled

a guest
May 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. function run($rootScope, $http, $location, $localStorage) {
  2. // keep user logged in after page refresh
  3. if ($localStorage.currentUser) {
  4. $http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.currentUser.token;
  5. }
  6.  
  7. // redirect to login page if not logged in and trying to access a restricted page
  8. $rootScope.$on('$locationChangeStart', function (event, next, current) {
  9. var publicPages = ['/login'];
  10. var restrictedPage = publicPages.indexOf($location.path()) === -1;
  11. if (restrictedPage && !$localStorage.currentUser) {
  12. $location.path('/login');
  13. }
  14. });
  15. }
  16.  
  17. (function () {
  18. 'use strict';
  19.  
  20. angular
  21. .module('app')
  22. .factory('AuthenticationService', Service);
  23.  
  24. function Service($http, $localStorage) {
  25. var service = {};
  26.  
  27. service.Login = Login;
  28. service.Logout = Logout;
  29.  
  30. return service;
  31.  
  32. function Login(username, password, callback) {
  33. $http.post('/api/authenticate', { username: username, password: password })
  34. .success(function (response) {
  35. // login successful if there's a token in the response
  36. if (response.token) {
  37. // store username and token in local storage to keep user logged in between page refreshes
  38. $localStorage.currentUser = { username: username, token: response.token };
  39.  
  40. // add jwt token to auth header for all requests made by the $http service
  41. $http.defaults.headers.common.Authorization = 'Bearer ' + response.token;
  42.  
  43. // execute callback with true to indicate successful login
  44. callback(true);
  45. } else {
  46. // execute callback with false to indicate failed login
  47. callback(false);
  48. }
  49. });
  50. }
  51.  
  52. function Logout() {
  53. // remove user from local storage and clear http auth header
  54. delete $localStorage.currentUser;
  55. $http.defaults.headers.common.Authorization = '';
  56. }
  57. }
  58. })();
  59.  
  60. function setupFakeBackend($httpBackend) {
  61. var testUser = { username: 'test', password: 'test', firstName: 'Test', lastName: 'User' };
  62.  
  63. // fake authenticate api end point
  64. $httpBackend.whenPOST('/api/authenticate').respond(function (method, url, data) {
  65. // get parameters from post request
  66. var params = angular.fromJson(data);
  67.  
  68. // check user credentials and return fake jwt token if valid
  69. if (params.username === testUser.username && params.password === testUser.password) {
  70. return [200, { token: 'fake-jwt-token' }, {}];
  71. } else {
  72. return [200, {}, {}];
  73. }
  74. });
  75.  
  76. // pass through any urls not handled above so static files are served correctly
  77. $httpBackend.whenGET(/^w+.*/).passThrough();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement