Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- angular.module('cognito.service', [])
- .factory('Cognito', function ($rootScope, $state, $q, $injector, $location, $config, ngDialog, toastr) {
- //AWS.config.region = $config.aws.region;
- var service = {};
- /*
- * Init
- *
- * Initialize Cognito services. Check if cognito user exists
- * in local storage and set root scope to authenticated
- */
- service.init = function(config) {
- Cognito.init(config);
- /*
- if(Cognito.cognitoUser) {
- $rootScope.authenticated = true;
- }
- */
- };
- service.setSession = function(token) {
- Cognito.setSession(token);
- };
- service.setCustomer = function(customer) {
- Cognito.setCustomer(customer);
- };
- /*
- * Login
- *
- * Login user and setup
- */
- service.login = function(credentials) {
- var deferred = $q.defer();
- if(!credentials.Username || !credentials.Password) {
- deferred.reject('Missing credentials.');
- } else {
- Cognito.login(credentials, function(result) {
- ngDialog.close();
- $rootScope.loaderStop();
- switch(result.action) {
- // On login success
- case 'login':
- deferred.resolve(result.data);
- break;
- // On new password required
- case 'new_password':
- service.setNewPasswordRequired(result.fn);
- break;
- // On login failure
- case 'failure':
- deferred.reject(result.data);
- break;
- // On missing credentials
- case 'missing_credentials':
- deferred.reject('Missing credentials.');
- break;
- }
- });
- }
- return deferred.promise;
- };
- service.setNewPasswordRequired = function(fn) {
- ngDialog.open({
- template: 'views/auth/templates/auth.dialog.verify.password.html',
- controller: ['$rootScope', '$scope', function($rootScope, $scope) {
- $scope.password = '';
- $scope.passwordConfirm = '';
- $scope.updatePassword = function() {
- if($scope.password == $scope.passwordConfirm) {
- if(Cognito.cognitoUser) {
- Cognito.cognitoUser.completeNewPasswordChallenge($scope.password, {}, fn);
- } else {
- toastr.error('Error completing new password.');
- }
- } else {
- toastr.error('Passwords do not match.');
- }
- };
- }]
- });
- };
- service.logout = function() {
- // doesnt need to be promise
- var deferred = $q.defer();
- Cognito.logout(function(success){
- deferred.resolve();
- });
- return deferred.promise;
- };
- /*
- * Get User
- *
- * Get current Cognito user
- */
- service.getUser = function() {
- return $config.user;
- };
- /*
- * Set App User Config
- *
- * Set application user config based on Cognito user attributes
- *
- */
- service.setUserConfig = function() {
- var deferred = $q.defer();
- Cognito.getUserAttributes(function(result) {
- if(result) {
- $config.user = result;
- deferred.resolve(result);
- } else {
- deferred.reject(result);
- }
- });
- return deferred.promise;
- };
- /*
- service.changePassword = function(oldPassword, newPassword) {
- var deferred = $q.defer();
- service.cognitoUser = service.userPool.getCurrentUser();
- service.isAuthenticated();
- if(service.cognitoUser) {
- service.cognitoUser.changePassword(oldPassword, newPassword, function(error, result) {
- if(error) { deferred.reject(error); }
- deferred.resolve(result);
- });
- }
- return deferred.promise;
- };
- service.setCustomAttributes = function(attributes) {
- var updatedAttributes = {};
- function _isCustomUserAttribute(attribute) {
- return attribute.match(/(custom:)/g) ? true : false;
- };
- for(var i in attributes) {
- var name = i;
- var value = attributes[i];
- if(_isCustomUserAttribute(name)) {
- var customAttribute = name.substring("custom:".length, name.length);
- updatedAttributes[i] = attributes[customAttribute];
- }
- }
- return updatedAttributes;
- };
- service.updateUserAttributes = function(attributes) {
- var deferred = $q.defer();
- var attributeList = [];
- var email = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute({Name: 'email', Value: attributes.email});
- attributeList.push(email);
- var customAttributes = service.setCustomAttributes(attributes);
- for(var i in customAttributes) {
- var name = i;
- var value = customAttributes[i];
- var attribute = {
- Name : name,
- Value : value
- };
- var attribute = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(attribute);
- attributeList.push(attribute);
- }
- service.cognitoUser.updateAttributes(attributeList, function(error, result) {
- if(error) { deferred.reject(error); }
- deferred.resolve(result);
- });
- return deferred.promise;
- };
- */
- /*
- * Is Session Valid
- *
- *
- */
- service.isSessionValid = function() {
- var deferred = $q.defer();
- Cognito.isSessionValid(function(result) {
- if(result) {
- deferred.resolve(result);
- } else {
- deferred.reject(result);
- }
- });
- return deferred.promise;
- };
- /*
- * Is Authenticated
- *
- * Check if user is authenticated by checking if session exists
- * in local storage
- *
- */
- service.isAuthenticated = function() {
- return Cognito.isAuthenticated();
- };
- return service;
- });
Add Comment
Please, Sign In to add comment