Skip to content

Commit 1d15cef

Browse files
committed
Added initiate auth.
1 parent 3fe95d2 commit 1d15cef

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/AuthenticationDetails.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ export default class AuthenticationDetails {
2323
* @param {string} data.Username User being authenticated.
2424
* @param {string} data.Password Plain-text password to authenticate with.
2525
* @param {(AttributeArg[])?} data.ValidationData Application extra metadata.
26+
* @param {(AttributeArg[])?} data.AuthParamaters Authentication paramaters for custom auth.
2627
*/
2728
constructor(data) {
28-
const { ValidationData, Username, Password } = data || {};
29+
const { ValidationData, Username, Password, AuthParameters } = data || {};
2930
this.validationData = ValidationData || [];
31+
this.authParameters = AuthParameters || [];
3032
this.username = Username;
3133
this.password = Password;
3234
}
@@ -51,4 +53,11 @@ export default class AuthenticationDetails {
5153
getValidationData() {
5254
return this.validationData;
5355
}
56+
57+
/**
58+
* @returns {Array} the record's authParameters
59+
*/
60+
getAuthParameters() {
61+
return this.authParameters;
62+
}
5463
}

src/CognitoUser.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,42 @@ export default class CognitoUser {
123123
this.authenticationFlowType = authenticationFlowType;
124124
}
125125

126+
/**
127+
* This is used for authenticating the user through the custom authentication flow.
128+
* @param {AuthenticationDetails} authDetails Contains the authentication data
129+
* @param {object} callback Result callback map.
130+
* @param {onFailure} callback.onFailure Called on any error.
131+
* @param {customChallenge} callback.customChallenge Custom challenge
132+
* response required to continue.
133+
* @param {authSuccess} callback.onSuccess Called on success with the new session.
134+
* @returns {void}
135+
*/
136+
initiateAuth(authDetails, callback) {
137+
const authParameters = authDetails.getAuthParameters();
138+
authParameters.USERNAME = this.username;
139+
140+
this.client.makeUnauthenticatedRequest('initiateAuth', {
141+
AuthFlow: 'CUSTOM_AUTH',
142+
ClientId: this.pool.getClientId(),
143+
AuthParameters: authParameters,
144+
ClientMetadata: authDetails.getValidationData(),
145+
}, (err, data) => {
146+
if (err) {
147+
return callback.onFailure(err);
148+
}
149+
const challengeName = data.ChallengeName;
150+
const challengeParameters = data.ChallengeParameters;
151+
152+
if (challengeName === 'CUSTOM_CHALLENGE') {
153+
this.Session = data.Session;
154+
return callback.customChallenge(challengeParameters);
155+
}
156+
this.signInUserSession = this.getCognitoUserSession(data.AuthenticationResult);
157+
this.cacheTokens();
158+
return callback.onSuccess(this.signInUserSession);
159+
});
160+
}
161+
126162
/**
127163
* This is used for authenticating the user. it calls the AuthenticationHelper for SRP related
128164
* stuff

0 commit comments

Comments
 (0)