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..17168a0 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,3 +1,4 @@ +const cloneDeep = require('lodash.clonedeep') import { CognitoUserPool, CognitoUserAttribute, @@ -68,13 +69,67 @@ 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) => { + 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; + }, };