Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify authenticateUser to handle the new callbacks. #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"license": "MIT",
"dependencies": {
"amazon-cognito-identity-js": "^1.19.0",
"lodash.clonedeep": "^4.5.0",
"vuex": "^2.3.1"
},
"devDependencies": {
Expand Down
59 changes: 57 additions & 2 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const cloneDeep = require('lodash.clonedeep')
import {
CognitoUserPool,
CognitoUserAttribute,
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions src/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
6 changes: 6 additions & 0 deletions src/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};