Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3. var app = angular.module('trainingPortal');
  4. app.factory('authService', ['$http', 'ngAuthSettings', '$q', 'localStorageService', function ($http, ngAuthSettings, $q, localStorageService) {
  5.  
  6. var serviceBase = ngAuthSettings.apiServiceBaseUri;
  7. var authServiceFactory = {};
  8.  
  9. var _authentication = {
  10. isAuth: false,
  11. userName: "",
  12. userRole: "",
  13. userApplicant: false,
  14. userTrainer: false,
  15. userTrainingOrganizer:false
  16. };
  17.  
  18. var _saveRegistration = function (registration) {
  19.  
  20. _logOut();
  21.  
  22. return $http.post(serviceBase + 'api/account/register', registration).then(function (response) {
  23. return response;
  24. });
  25.  
  26. };
  27.  
  28. var _login = function (loginData) {
  29.  
  30. var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
  31.  
  32. var deferred = $q.defer();
  33.  
  34. $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
  35.  
  36. localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, userRole: response.userRole });
  37.  
  38. _authentication.isAuth = true;
  39. _authentication.userName = loginData.userName;
  40. _authentication.userRole = response.userRole;
  41. if (_authentication.userRole.indexOf("Trainer") != -1)
  42. _authentication.userTrainer = true;
  43. if (_authentication.userRole.indexOf("Training Organizer") != -1)
  44. _authentication.userTrainingOrganizer = true;
  45. if (!_authentication.userTrainer && !_authentication.userTrainingOrganizer)
  46. _authentication.userApplicant = true;
  47. deferred.resolve(response);
  48.  
  49. }).error(function (err, status) {
  50. _logOut();
  51. deferred.reject(err);
  52. });
  53.  
  54. return deferred.promise;
  55.  
  56. };
  57.  
  58. var _logOut = function () {
  59.  
  60. localStorageService.remove('authorizationData');
  61.  
  62. _authentication.isAuth = false;
  63. _authentication.userName = "";
  64. _authentication.userRole = "";
  65. _authentication.userApplicant = false;
  66. _authentication.userTrainer = false;
  67. _authentication.userTrainingOrganizer = false;
  68.  
  69. };
  70.  
  71. var _fillAuthData = function () {
  72.  
  73. var authData = localStorageService.get('authorizationData');
  74. if (authData) {
  75. _authentication.isAuth = true;
  76. _authentication.userName = authData.userName;
  77. _authentication.userRole = authData.userRole;
  78. if (_authentication.userRole.indexOf("Trainer") != -1)
  79. _authentication.userTrainer = true;
  80. if (_authentication.userRole.indexOf("Training Organizer") != -1)
  81. _authentication.userTrainingOrganizer = true;
  82. if (!_authentication.userTrainer && !_authentication.userTrainingOrganizer)
  83. _authentication.userApplicant = true;
  84. }
  85.  
  86. }
  87.  
  88. authServiceFactory.saveRegistration = _saveRegistration;
  89. authServiceFactory.login = _login;
  90. authServiceFactory.logOut = _logOut;
  91. authServiceFactory.fillAuthData = _fillAuthData;
  92. authServiceFactory.authentication = _authentication;
  93.  
  94. return authServiceFactory;
  95. }]);
  96. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement