@@ -18,7 +18,8 @@ import {
18
18
FederatedUser ,
19
19
ConfirmSignUpOptions ,
20
20
SignOutOpts ,
21
- CurrentUserOpts
21
+ CurrentUserOpts ,
22
+ SignInOpts
22
23
} from './types' ;
23
24
24
25
import {
@@ -304,18 +305,36 @@ export default class AuthClass {
304
305
305
306
/**
306
307
* Sign in
307
- * @param {String } username - The username to be signed in
308
+ * @param {String | Object } username - The username to be signed in
308
309
* @param {String } password - The password of the username
309
310
* @return - A promise resolves the CognitoUser
310
311
*/
311
- public signIn ( username : string , password ?: string ) : Promise < CognitoUser | any > {
312
+ public signIn ( signInOpts : string | SignInOpts , pw ?: string ) : Promise < CognitoUser | any > {
312
313
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
+ }
313
328
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
+ } ) ;
315
334
if ( password ) {
316
- return this . signInWithPassword ( username , password ) ;
335
+ return this . signInWithPassword ( authDetails ) ;
317
336
} else {
318
- return this . signInWithoutPassword ( username ) ;
337
+ return this . signInWithoutPassword ( authDetails ) ;
319
338
}
320
339
}
321
340
@@ -401,12 +420,8 @@ export default class AuthClass {
401
420
* @param {String } password - The password of the username
402
421
* @return - A promise resolves the CognitoUser object if success or mfa required
403
422
*/
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 ( ) ) ;
410
425
411
426
return new Promise ( ( resolve , reject ) => {
412
427
user . authenticateUser ( authDetails , this . authCallbacks ( user , resolve , reject ) ) ;
@@ -418,12 +433,9 @@ export default class AuthClass {
418
433
* @param {String } username - The username to be signed in
419
434
* @return - A promise resolves the CognitoUser object if success or mfa required
420
435
*/
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 ( ) ) ;
423
438
user . setAuthenticationFlowType ( 'CUSTOM_AUTH' ) ;
424
- const authDetails = new AuthenticationDetails ( {
425
- Username : username
426
- } ) ;
427
439
428
440
return new Promise ( ( resolve , reject ) => {
429
441
user . initiateAuth ( authDetails , this . authCallbacks ( user , resolve , reject ) ) ;
0 commit comments