Guest User

Untitled

a guest
Apr 14th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('cognito.service', [])
  4.  
  5. .factory('Cognito', function ($rootScope, $state, $q, $injector, $location, $config, ngDialog, toastr) {
  6.  
  7. //AWS.config.region = $config.aws.region;
  8.  
  9. var service = {};
  10.  
  11. /*
  12. * Init
  13. *
  14. * Initialize Cognito services. Check if cognito user exists
  15. * in local storage and set root scope to authenticated
  16. */
  17. service.init = function(config) {
  18. Cognito.init(config);
  19. /*
  20. if(Cognito.cognitoUser) {
  21. $rootScope.authenticated = true;
  22. }
  23. */
  24. };
  25.  
  26. service.setSession = function(token) {
  27. Cognito.setSession(token);
  28. };
  29.  
  30. service.setCustomer = function(customer) {
  31. Cognito.setCustomer(customer);
  32. };
  33.  
  34. /*
  35. * Login
  36. *
  37. * Login user and setup
  38. */
  39. service.login = function(credentials) {
  40. var deferred = $q.defer();
  41.  
  42. if(!credentials.Username || !credentials.Password) {
  43. deferred.reject('Missing credentials.');
  44. } else {
  45. Cognito.login(credentials, function(result) {
  46. ngDialog.close();
  47. $rootScope.loaderStop();
  48.  
  49. switch(result.action) {
  50.  
  51. // On login success
  52. case 'login':
  53. deferred.resolve(result.data);
  54. break;
  55.  
  56. // On new password required
  57. case 'new_password':
  58. service.setNewPasswordRequired(result.fn);
  59. break;
  60.  
  61. // On login failure
  62. case 'failure':
  63. deferred.reject(result.data);
  64. break;
  65.  
  66. // On missing credentials
  67. case 'missing_credentials':
  68. deferred.reject('Missing credentials.');
  69. break;
  70. }
  71. });
  72.  
  73. }
  74. return deferred.promise;
  75. };
  76.  
  77. service.setNewPasswordRequired = function(fn) {
  78. ngDialog.open({
  79. template: 'views/auth/templates/auth.dialog.verify.password.html',
  80. controller: ['$rootScope', '$scope', function($rootScope, $scope) {
  81. $scope.password = '';
  82. $scope.passwordConfirm = '';
  83. $scope.updatePassword = function() {
  84. if($scope.password == $scope.passwordConfirm) {
  85. if(Cognito.cognitoUser) {
  86. Cognito.cognitoUser.completeNewPasswordChallenge($scope.password, {}, fn);
  87. } else {
  88. toastr.error('Error completing new password.');
  89. }
  90. } else {
  91. toastr.error('Passwords do not match.');
  92. }
  93. };
  94. }]
  95. });
  96. };
  97.  
  98. service.logout = function() {
  99. // doesnt need to be promise
  100. var deferred = $q.defer();
  101. Cognito.logout(function(success){
  102. deferred.resolve();
  103. });
  104. return deferred.promise;
  105. };
  106.  
  107. /*
  108. * Get User
  109. *
  110. * Get current Cognito user
  111. */
  112. service.getUser = function() {
  113. return $config.user;
  114. };
  115.  
  116. /*
  117. * Set App User Config
  118. *
  119. * Set application user config based on Cognito user attributes
  120. *
  121. */
  122. service.setUserConfig = function() {
  123. var deferred = $q.defer();
  124. Cognito.getUserAttributes(function(result) {
  125. if(result) {
  126. $config.user = result;
  127. deferred.resolve(result);
  128. } else {
  129. deferred.reject(result);
  130. }
  131. });
  132. return deferred.promise;
  133. };
  134.  
  135. /*
  136.  
  137.  
  138. service.changePassword = function(oldPassword, newPassword) {
  139. var deferred = $q.defer();
  140. service.cognitoUser = service.userPool.getCurrentUser();
  141. service.isAuthenticated();
  142. if(service.cognitoUser) {
  143. service.cognitoUser.changePassword(oldPassword, newPassword, function(error, result) {
  144. if(error) { deferred.reject(error); }
  145. deferred.resolve(result);
  146. });
  147. }
  148. return deferred.promise;
  149. };
  150.  
  151.  
  152. service.setCustomAttributes = function(attributes) {
  153. var updatedAttributes = {};
  154.  
  155. function _isCustomUserAttribute(attribute) {
  156. return attribute.match(/(custom:)/g) ? true : false;
  157. };
  158.  
  159. for(var i in attributes) {
  160. var name = i;
  161. var value = attributes[i];
  162. if(_isCustomUserAttribute(name)) {
  163. var customAttribute = name.substring("custom:".length, name.length);
  164. updatedAttributes[i] = attributes[customAttribute];
  165. }
  166. }
  167. return updatedAttributes;
  168. };
  169.  
  170. service.updateUserAttributes = function(attributes) {
  171. var deferred = $q.defer();
  172. var attributeList = [];
  173.  
  174. var email = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute({Name: 'email', Value: attributes.email});
  175. attributeList.push(email);
  176.  
  177. var customAttributes = service.setCustomAttributes(attributes);
  178. for(var i in customAttributes) {
  179. var name = i;
  180. var value = customAttributes[i];
  181. var attribute = {
  182. Name : name,
  183. Value : value
  184. };
  185. var attribute = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(attribute);
  186. attributeList.push(attribute);
  187. }
  188.  
  189. service.cognitoUser.updateAttributes(attributeList, function(error, result) {
  190. if(error) { deferred.reject(error); }
  191. deferred.resolve(result);
  192. });
  193. return deferred.promise;
  194. };
  195. */
  196.  
  197. /*
  198. * Is Session Valid
  199. *
  200. *
  201. */
  202. service.isSessionValid = function() {
  203. var deferred = $q.defer();
  204. Cognito.isSessionValid(function(result) {
  205. if(result) {
  206. deferred.resolve(result);
  207. } else {
  208. deferred.reject(result);
  209. }
  210. });
  211. return deferred.promise;
  212. };
  213.  
  214. /*
  215. * Is Authenticated
  216. *
  217. * Check if user is authenticated by checking if session exists
  218. * in local storage
  219. *
  220. */
  221. service.isAuthenticated = function() {
  222. return Cognito.isAuthenticated();
  223. };
  224.  
  225. return service;
  226.  
  227. });
Add Comment
Please, Sign In to add comment