-
Notifications
You must be signed in to change notification settings - Fork 582
/
authenticateWithCognito.js
58 lines (50 loc) · 1.93 KB
/
authenticateWithCognito.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const AmazonCognitoIdentity = require("amazon-cognito-identity-js");
global.fetch = require("node-fetch");
const AWS_COGNITO = {
USER_POOL_ID: Cypress.env("AWS_COGNITO_USER_POOL_ID"),
CLIENT_ID: Cypress.env("AWS_COGNITO_CLIENT_ID")
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool({
UserPoolId: AWS_COGNITO.USER_POOL_ID,
ClientId: AWS_COGNITO.CLIENT_ID
});
export default ({ username, password }) => {
const userData = {
Username: username,
Pool: userPool
};
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
Username: username,
Password: password
});
return new Promise((resolve, reject) => {
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: resolve,
onFailure: function (err) {
console.log(
`An error occurred while executing login command ("cognitoUser.authenticateUser")`,
err
);
reject(err);
},
newPasswordRequired: function (userAttributes) {
delete userAttributes.email_verified; // it's returned but not valid to submit
const newPassword = "12345678";
userAttributes.email = username;
cognitoUser.completeNewPasswordChallenge(newPassword, userAttributes, {
onSuccess: resolve,
onFailure: function (err) {
console.log(
`An error occurred while executing login command ("cognitoUser.completeNewPasswordChallenge")`,
err,
userAttributes
);
reject(err);
}
});
}
});
return cognitoUser;
});
};