Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- angular.module('mssPortal').service('authService', ['$rootScope', '$resource', '$state', '$timeout', 'authManager', '$q',
- function ($rootScope, $resource, $state, $timeout, authManager, $q) {
- var authService = this;
- authService.cognitoUser = null;
- authService.username = '';
- authService.password = '';
- authService.newPassword1 = '';
- authService.newPassword2 = '';
- authService.userProfile = {};
- authService.loginError = '';
- authService.newUser = false;
- authService.mfaPrompt = false;
- authService.mfaCode = '';
- authService.login = function () {
- var authenticationData = {
- Username: authService.username,
- Password: authService.password
- };
- var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
- var poolData = {
- UserPoolId: COGNITO_USER_POOL_ID,
- ClientId: COGNITO_CLIENT_ID
- };
- var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
- var userData = {
- Username: authService.username,
- Pool: userPool
- };
- authService.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
- authService.cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- authManager.authenticate();
- localStorage.setItem('id_token', result.getIdToken().getJwtToken());
- var username = JSON.parse(atob(result.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
- authService.cognitoUser.getUserAttributes(function(err, result) {
- if (err) {
- alert(err);
- return;
- }
- var profile = {};
- _.each(result, function(a){
- profile[a.Name] = a.Value;
- });
- profile["username"] = username;
- localStorage.setItem('profile', JSON.stringify(profile));
- authService.userProfile = profile;
- });
- authService.username = '';
- authService.password = '';
- $rootScope.$apply();
- $state.go('dashboard'); // CUSTOM LANDING PAGE CAN GO HERE
- },
- onFailure: function (err) {
- authService.loginError = err.message;
- $rootScope.$apply();
- },
- mfaRequired: function(codeDeliveryDetails) {
- authService.mfaPrompt = true;
- $rootScope.$apply();
- },
- newPasswordRequired: function(userAttributes, requiredAttributes) {
- authService.newUser = true;
- $rootScope.$apply();
- }
- });
- };
- authService.forgotPassword = function(username){
- var poolData = {
- UserPoolId: COGNITO_USER_POOL_ID,
- ClientId: COGNITO_CLIENT_ID
- };
- var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
- var userData = {
- Username: username,
- Pool: userPool
- };
- authService.cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
- authService.cognitoUser.forgotPassword({
- onSuccess: function () {
- // successfully initiated reset password request
- },
- onFailure: function(err) {
- alert(err);
- },
- inputVerificationCode: function(data) {
- console.log('Code sent to: ' + data.toString());
- var verificationCode = prompt('Please input verification code ' ,'');
- var newPassword = prompt('Enter new password ' ,'');
- authService.cognitoUser.confirmPassword(verificationCode, newPassword, this);
- }
- });
- };
- authService.newUserFlow = function(passwordMatch){
- if (passwordMatch) {
- // Get these details and call
- // newPassword: password that user has given
- // attributesData: object with key as attribute name and value that the user has given.
- authService.cognitoUser.completeNewPasswordChallenge(authService.newPassword2, null, {
- onSuccess: function(result) {
- authService.newPassword1 = '';
- authService.newPassword2 = '';
- authService.loginError = '';
- authService.newUser = false;
- $rootScope.$apply();
- },
- onFailure: function(error) {
- authService.loginError = error.message;
- $rootScope.$apply();
- }
- });
- }
- else {
- authService.loginError = "Passwords do not match"
- }
- };
- authService.mfaFlow = function() {
- authService.cognitoUser.sendMFACode(authService.mfaCode, {
- onSuccess: function (result) {
- authManager.authenticate();
- localStorage.setItem('id_token', result.getIdToken().getJwtToken());
- authService.cognitoUser.getUserAttributes(function(err, result) {
- if (err) {
- alert(err);
- return;
- }
- var username = JSON.parse(atob(result.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
- var profile = {};
- _.each(result, function(a){
- profile[a.Name] = a.Value;
- });
- profile["username"] = username;
- localStorage.setItem('profile', JSON.stringify(profile));
- authService.userProfile = profile;
- });
- authService.username = '';
- authService.password = '';
- $rootScope.$apply();
- $state.go('dashboard'); // CUSTOM LANDING PAGE CAN GO HERE
- },
- onFailure: function (error) {
- authService.loginError = error.message;
- $rootScope.$apply();
- }
- })
- };
- authService.checkSession = function(){
- var deferred = $q.defer();
- var poolData = {
- UserPoolId: COGNITO_USER_POOL_ID,
- ClientId: COGNITO_CLIENT_ID
- };
- var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
- authService.cognitoUser = userPool.getCurrentUser();
- if (authService.cognitoUser != null) {
- authService.cognitoUser.getSession(function(err, session) {
- if (err) {
- console.log(err);
- deferred.reject();
- }
- localStorage.setItem('id_token', session.getIdToken().getJwtToken());
- authService.cognitoUser.getUserAttributes(function(err, result) {
- if (err) {
- console.log(err);
- authService.logout();
- deferred.reject();
- }
- var username = JSON.parse(atob(session.getIdToken().getJwtToken().split('.')[1]))["cognito:username"];
- var profile = {};
- _.each(result, function(a){
- profile[a.Name] = a.Value;
- });
- profile["username"] = username;
- localStorage.setItem('profile', JSON.stringify(profile));
- authService.userProfile = profile;
- });
- var valid = session.isValid();
- console.log('Session valid: ', valid);
- if (valid) {
- deferred.resolve(valid)
- }
- else{
- authService.logout();
- deferred.reject();
- }
- });
- }
- else {
- authService.logout();
- deferred.reject();
- }
- return deferred.promise;
- };
- authService.logout = function () {
- console.log('*********************** Log out *********************')
- var poolData = {
- UserPoolId: COGNITO_USER_POOL_ID,
- ClientId: COGNITO_CLIENT_ID
- };
- var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
- authService.cognitoUser = userPool.getCurrentUser();
- if (authService.cognitoUser != null) {
- authService.cognitoUser.signOut();
- }
- authManager.unauthenticate();
- authService.userProfile = {};
- localStorage.removeItem('id_token');
- localStorage.removeItem('profile');
- $state.go('root');
- };
- }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement