From 9695a208ed648d9357f98e18a12e50a1f66163a3 Mon Sep 17 00:00:00 2001 From: joelkleppinger Date: Fri, 6 Oct 2017 01:06:15 +0000 Subject: [PATCH 1/2] Modify authenticateUser to handle the new callbacks. Add completeNewPasswordChallenge for users created by the admin so users can set their password and be logged in. Required adding lodash.clonedeep to persist the cognitoUser object --- package.json | 1 + src/actions.js | 60 +++++++++++++++++++++++++++++++++++++++++-- src/mutation-types.js | 2 ++ src/mutations.js | 6 +++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f5c703f..a984312 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "license": "MIT", "dependencies": { "amazon-cognito-identity-js": "^1.19.0", + "lodash.clonedeep": "^4.5.0", "vuex": "^2.3.1" }, "devDependencies": { diff --git a/src/actions.js b/src/actions.js index a6263cc..1cd3d9a 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,3 +1,4 @@ +const cloneDeep = require('lodash.clonedeep') import { CognitoUserPool, CognitoUserAttribute, @@ -68,13 +69,68 @@ export default function actionsFactory(config) { onFailure: (err) => { reject(err); }, - onSuccess: (session, userConfirmationNecessary) => { + onSuccess: (session) => { commit(types.AUTHENTICATE, constructUser(cognitoUser, session)); - resolve({ userConfirmationNecessary }); + resolve({ userConfirmationNecessary: false }); }, + mfaRequired: function(codeDeliveryDetails) { + // @todo MFA not implemented yet + // MFA Needs a sendMFACode function similar to completeNewPasswordChallenge + // MFA is required to complete user authentication. + // Get the code from user and call + // cognitoUser.sendMFACode(mfaCode, this) + }, + + newPasswordRequired: function(userAttributes, requiredAttributes) { + // User was signed up by an admin and must provide new + // password and required attributes, if any, to complete + // authentication. + + // userAttributes: object, which is the user's current profile. It will list all attributes that are associated with the user. + // Required attributes according to schema, which don’t have any values yet, will have blank values. + // requiredAttributes: list of attributes that must be set by the user along with new password to complete the sign-in. + + // Get the new password and any required attributes into a format similar to userAttributes + // Then call completeNewPasswordChallenge + + delete userAttributes.email_verified; // it's returned but not valid to submit + + //Store the cognitoUser object in order to reuse it + commit(types.COGNITOUSER, cognitoUser); + + resolve({ userConfirmationNecessary: true, userAttributes: userAttributes, requiredAttributes: requiredAttributes }); + } })); }, + completeNewPasswordChallenge({ commit, state }, payload) { + console.log ('in-function') + // const cognitoUser = Object.assign({}, state.cognito.cognitoUser); + const cognitoUser = cloneDeep(state.cognitoUser); + // const cognitoUser = state.cognitoUser + + return new Promise((resolve, reject) => { + if (cognitoUser === null) { + reject({ + message: 'User is unauthenticated', + }); + return; + } + + cognitoUser.completeNewPasswordChallenge(payload.newPassword, payload.userAttributes, { + onFailure: (err) => { + // console.log(err); + reject(err); + }, + onSuccess: (session) => { + commit(types.AUTHENTICATE, constructUser(cognitoUser, session)); + commit(types.REMOVECOGNITOUSER); + resolve(); + } + }) + }); + }, + signUp({ commit }, userInfo) { /* userInfo: { username, password, attributes } */ const userAttributes = Object.keys(userInfo.attributes || {}).map(key => new CognitoUserAttribute({ diff --git a/src/mutation-types.js b/src/mutation-types.js index a977aa7..dcc0706 100644 --- a/src/mutation-types.js +++ b/src/mutation-types.js @@ -3,3 +3,5 @@ const prefix = 'vue-auth-cognito'; export const AUTHENTICATE = `${prefix}/AUTHENTICATE`; export const ATTRIBUTES = `${prefix}/ATTRIBUTES`; export const SIGNOUT = `${prefix}/SIGNOUT`; +export const COGNITOUSER = `${prefix}/COGNITOUSER`; +export const REMOVECOGNITOUSER = `${prefix}/REMOVECOGNITOUSER`; diff --git a/src/mutations.js b/src/mutations.js index ea6558a..b00f8d0 100644 --- a/src/mutations.js +++ b/src/mutations.js @@ -10,4 +10,10 @@ export default { [types.ATTRIBUTES](state, payload) { state.user.attributes = payload; }, + [types.COGNITOUSER](state, payload) { + state.cognitoUser = payload; + }, + [types.REMOVECOGNITOUSER](state) { + state.cognitoUser = null; + }, }; From 6a09dc3713d7c4285ce1d6e0ac31a7880332eb3c Mon Sep 17 00:00:00 2001 From: joelkleppinger Date: Wed, 18 Oct 2017 17:53:32 +0000 Subject: [PATCH 2/2] Minor code formatting cleanup --- src/actions.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/actions.js b/src/actions.js index 1cd3d9a..17168a0 100644 --- a/src/actions.js +++ b/src/actions.js @@ -73,7 +73,7 @@ export default function actionsFactory(config) { commit(types.AUTHENTICATE, constructUser(cognitoUser, session)); resolve({ userConfirmationNecessary: false }); }, - mfaRequired: function(codeDeliveryDetails) { + mfaRequired: function (codeDeliveryDetails) { // @todo MFA not implemented yet // MFA Needs a sendMFACode function similar to completeNewPasswordChallenge // MFA is required to complete user authentication. @@ -81,7 +81,7 @@ export default function actionsFactory(config) { // cognitoUser.sendMFACode(mfaCode, this) }, - newPasswordRequired: function(userAttributes, requiredAttributes) { + newPasswordRequired: function (userAttributes, requiredAttributes) { // User was signed up by an admin and must provide new // password and required attributes, if any, to complete // authentication. @@ -119,7 +119,6 @@ export default function actionsFactory(config) { cognitoUser.completeNewPasswordChallenge(payload.newPassword, payload.userAttributes, { onFailure: (err) => { - // console.log(err); reject(err); }, onSuccess: (session) => {