Skip to content

Commit 13093e9

Browse files
committed
feat(@aws-amplify/auth): add the option to pass validation data when signing in
1 parent b2d4364 commit 13093e9

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

packages/amazon-cognito-identity-js/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ declare module "amazon-cognito-identity-js" {
2222
export interface IAuthenticationDetailsData {
2323
Username: string;
2424
Password?: string;
25+
ValidationData?: {[key: string]: any}
2526
}
2627

2728
export class AuthenticationDetails {

packages/auth/src/Auth.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
FederatedUser,
1919
ConfirmSignUpOptions,
2020
SignOutOpts,
21-
CurrentUserOpts
21+
CurrentUserOpts,
22+
SignInOpts
2223
} from './types';
2324

2425
import {
@@ -304,18 +305,36 @@ export default class AuthClass {
304305

305306
/**
306307
* Sign in
307-
* @param {String} username - The username to be signed in
308+
* @param {String | Object} username - The username to be signed in
308309
* @param {String} password - The password of the username
309310
* @return - A promise resolves the CognitoUser
310311
*/
311-
public signIn(username: string, password?: string): Promise<CognitoUser | any> {
312+
public signIn(signInOpts: string | SignInOpts, pw?: string): Promise<CognitoUser | any> {
312313
if (!this.userPool) { return Promise.reject('No userPool'); }
314+
let username = null;
315+
let password = null;
316+
let validationData = {};
317+
// for backward compatibility
318+
if (typeof signInOpts === 'string') {
319+
username = signInOpts;
320+
password = pw;
321+
} else if (typeof signInOpts === 'object') {
322+
username = signInOpts.username;
323+
password = signInOpts.password;
324+
validationData = signInOpts.validationData;
325+
} else {
326+
return Promise.reject(new Error('sign in parameters should not be undefined'));
327+
}
313328
if (!username) { return Promise.reject('Username cannot be empty'); }
314-
329+
const authDetails = new AuthenticationDetails({
330+
Username: username,
331+
Password: password,
332+
ValidationData: validationData
333+
});
315334
if (password) {
316-
return this.signInWithPassword(username, password);
335+
return this.signInWithPassword(authDetails);
317336
} else {
318-
return this.signInWithoutPassword(username);
337+
return this.signInWithoutPassword(authDetails);
319338
}
320339
}
321340

@@ -401,12 +420,8 @@ export default class AuthClass {
401420
* @param {String} password - The password of the username
402421
* @return - A promise resolves the CognitoUser object if success or mfa required
403422
*/
404-
private signInWithPassword(username: string, password: string): Promise<CognitoUser | any> {
405-
const user = this.createCognitoUser(username);
406-
const authDetails = new AuthenticationDetails({
407-
Username: username,
408-
Password: password
409-
});
423+
private signInWithPassword(authDetails: AuthenticationDetails): Promise<CognitoUser | any> {
424+
const user = this.createCognitoUser(authDetails.getUsername());
410425

411426
return new Promise((resolve, reject) => {
412427
user.authenticateUser(authDetails, this.authCallbacks(user, resolve, reject));
@@ -418,12 +433,9 @@ export default class AuthClass {
418433
* @param {String} username - The username to be signed in
419434
* @return - A promise resolves the CognitoUser object if success or mfa required
420435
*/
421-
private signInWithoutPassword(username: string): Promise<CognitoUser | any> {
422-
const user = this.createCognitoUser(username);
436+
private signInWithoutPassword(authDetails: AuthenticationDetails): Promise<CognitoUser | any> {
437+
const user = this.createCognitoUser(authDetails.getUsername());
423438
user.setAuthenticationFlowType('CUSTOM_AUTH');
424-
const authDetails = new AuthenticationDetails({
425-
Username: username
426-
});
427439

428440
return new Promise((resolve, reject) => {
429441
user.initiateAuth(authDetails, this.authCallbacks(user, resolve, reject));

packages/auth/src/types/Auth.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ export interface SignOutOpts {
9999
export interface CurrentUserOpts {
100100
bypassCache: boolean
101101
}
102+
103+
export interface SignInOpts {
104+
username?: string,
105+
password?: string,
106+
validationData?: {[key:string]: any}
107+
}

0 commit comments

Comments
 (0)