Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('mssPortal').service('authService', ['$rootScope', '$resource', '$state', '$timeout', 'authManager', '$q',
  4. function ($rootScope, $resource, $state, $timeout, authManager, $q) {
  5.  
  6. var authService = this;
  7. authService.cognitoUser = null;
  8. authService.username = '';
  9. authService.password = '';
  10. authService.newPassword1 = '';
  11. authService.newPassword2 = '';
  12. authService.userProfile = {};
  13. authService.loginError = '';
  14. authService.newUser = false;
  15. authService.mfaPrompt = false;
  16. authService.mfaCode = '';
  17.  
  18.  
  19. authService.login = function () {
  20. var authenticationData = {
  21. Username: authService.username,
  22. Password: authService.password
  23. };
  24.  
  25. var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
  26.  
  27. var poolData = {
  28. UserPoolId: COGNITO_USER_POOL_ID,
  29. ClientId: COGNITO_CLIENT_ID
  30. };
  31.  
  32. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  33.  
  34. var userData = {
  35. Username: authService.username,
  36. Pool: userPool
  37. };
  38.  
  39. authService.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  40.  
  41. authService.cognitoUser.authenticateUser(authenticationDetails, {
  42. onSuccess: function (result) {
  43.  
  44. authManager.authenticate();
  45. localStorage.setItem('id_token', result.getIdToken().getJwtToken());
  46.  
  47. var username = JSON.parse(atob(result.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
  48.  
  49. authService.cognitoUser.getUserAttributes(function(err, result) {
  50. if (err) {
  51. alert(err);
  52. return;
  53. }
  54.  
  55. var profile = {};
  56. _.each(result, function(a){
  57. profile[a.Name] = a.Value;
  58. });
  59. profile["username"] = username;
  60. localStorage.setItem('profile', JSON.stringify(profile));
  61. authService.userProfile = profile;
  62. });
  63. authService.username = '';
  64. authService.password = '';
  65. $rootScope.$apply();
  66. $state.go('dashboard'); // CUSTOM LANDING PAGE CAN GO HERE
  67. },
  68.  
  69. onFailure: function (err) {
  70. authService.loginError = err.message;
  71. $rootScope.$apply();
  72. },
  73.  
  74. mfaRequired: function(codeDeliveryDetails) {
  75. authService.mfaPrompt = true;
  76. $rootScope.$apply();
  77. },
  78.  
  79. newPasswordRequired: function(userAttributes, requiredAttributes) {
  80. authService.newUser = true;
  81. $rootScope.$apply();
  82. }
  83.  
  84. });
  85. };
  86.  
  87. authService.forgotPassword = function(username){
  88.  
  89. var poolData = {
  90. UserPoolId: COGNITO_USER_POOL_ID,
  91. ClientId: COGNITO_CLIENT_ID
  92. };
  93.  
  94. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  95.  
  96. var userData = {
  97. Username: username,
  98. Pool: userPool
  99. };
  100.  
  101. authService.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  102.  
  103. authService.cognitoUser.forgotPassword({
  104. onSuccess: function () {
  105. // successfully initiated reset password request
  106. },
  107. onFailure: function(err) {
  108. alert(err);
  109. },
  110. inputVerificationCode: function(data) {
  111. console.log('Code sent to: ' + data.toString());
  112. var verificationCode = prompt('Please input verification code ' ,'');
  113. var newPassword = prompt('Enter new password ' ,'');
  114. authService.cognitoUser.confirmPassword(verificationCode, newPassword, this);
  115. }
  116. });
  117. };
  118.  
  119. authService.newUserFlow = function(passwordMatch){
  120. if (passwordMatch) {
  121. // Get these details and call
  122. // newPassword: password that user has given
  123. // attributesData: object with key as attribute name and value that the user has given.
  124. authService.cognitoUser.completeNewPasswordChallenge(authService.newPassword2, null, {
  125. onSuccess: function(result) {
  126. authService.newPassword1 = '';
  127. authService.newPassword2 = '';
  128. authService.loginError = '';
  129. authService.newUser = false;
  130. $rootScope.$apply();
  131.  
  132. },
  133. onFailure: function(error) {
  134. authService.loginError = error.message;
  135. $rootScope.$apply();
  136. }
  137. });
  138.  
  139. }
  140. else {
  141. authService.loginError = "Passwords do not match"
  142. }
  143. };
  144.  
  145. authService.mfaFlow = function() {
  146. authService.cognitoUser.sendMFACode(authService.mfaCode, {
  147. onSuccess: function (result) {
  148. authManager.authenticate();
  149. localStorage.setItem('id_token', result.getIdToken().getJwtToken());
  150. authService.cognitoUser.getUserAttributes(function(err, result) {
  151. if (err) {
  152. alert(err);
  153. return;
  154. }
  155. var username = JSON.parse(atob(result.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
  156. var profile = {};
  157. _.each(result, function(a){
  158. profile[a.Name] = a.Value;
  159. });
  160. profile["username"] = username;
  161. localStorage.setItem('profile', JSON.stringify(profile));
  162. authService.userProfile = profile;
  163. });
  164. authService.username = '';
  165. authService.password = '';
  166. $rootScope.$apply();
  167. $state.go('dashboard'); // CUSTOM LANDING PAGE CAN GO HERE
  168. },
  169. onFailure: function (error) {
  170. authService.loginError = error.message;
  171. $rootScope.$apply();
  172. }
  173. })
  174. };
  175.  
  176. authService.checkSession = function(){
  177. var deferred = $q.defer();
  178.  
  179. var poolData = {
  180. UserPoolId: COGNITO_USER_POOL_ID,
  181. ClientId: COGNITO_CLIENT_ID
  182. };
  183.  
  184. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  185. authService.cognitoUser = userPool.getCurrentUser();
  186.  
  187. if (authService.cognitoUser != null) {
  188. authService.cognitoUser.getSession(function(err, session) {
  189. if (err) {
  190. console.log(err);
  191. deferred.reject();
  192. }
  193. localStorage.setItem('id_token', session.getIdToken().getJwtToken());
  194. authService.cognitoUser.getUserAttributes(function(err, result) {
  195. if (err) {
  196. console.log(err);
  197. authService.logout();
  198. deferred.reject();
  199. }
  200. var username = JSON.parse(atob(session.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
  201. var profile = {};
  202. _.each(result, function(a){
  203. profile[a.Name] = a.Value;
  204. });
  205. profile["username"] = username;
  206. localStorage.setItem('profile', JSON.stringify(profile));
  207. authService.userProfile = profile;
  208. });
  209. var valid = session.isValid();
  210. console.log('Session valid: ', valid);
  211. if (valid) {
  212. deferred.resolve(valid)
  213. }
  214. else{
  215. authService.logout();
  216. deferred.reject();
  217. }
  218. });
  219. }
  220. else {
  221. authService.logout();
  222. deferred.reject();
  223. }
  224. return deferred.promise;
  225. };
  226.  
  227. authService.logout = function () {
  228. console.log('*********************** Log out *********************')
  229. var poolData = {
  230. UserPoolId: COGNITO_USER_POOL_ID,
  231. ClientId: COGNITO_CLIENT_ID
  232. };
  233. var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
  234.  
  235. authService.cognitoUser = userPool.getCurrentUser();
  236.  
  237. if (authService.cognitoUser != null) {
  238. authService.cognitoUser.signOut();
  239. }
  240.  
  241. authManager.unauthenticate();
  242. authService.userProfile = {};
  243. localStorage.removeItem('id_token');
  244. localStorage.removeItem('profile');
  245. $state.go('root');
  246. };
  247. }
  248. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement