@aws-amplify/auth 4.5.11-unstable.2 → 4.5.11-unstable.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,7 @@ export var AuthErrorTypes;
49
49
  AuthErrorTypes["Default"] = "default";
50
50
  AuthErrorTypes["DeviceConfig"] = "deviceConfig";
51
51
  AuthErrorTypes["NetworkError"] = "networkError";
52
+ AuthErrorTypes["AutoSignInError"] = "autoSignInError";
52
53
  })(AuthErrorTypes || (AuthErrorTypes = {}));
53
54
  export function isUsernamePasswordOpts(obj) {
54
55
  return !!obj.username;
@@ -1 +1 @@
1
- {"version":3,"file":"Auth.js","sourceRoot":"","sources":["../../src/types/Auth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA2CH,MAAM,CAAN,IAAY,+BAMX;AAND,WAAY,+BAA+B;IAC1C,sDAAmB,CAAA;IACnB,oDAAiB,CAAA;IACjB,wDAAqB,CAAA;IACrB,6DAA0B,CAAA;IAC1B,4DAAyB,CAAA;AAC1B,CAAC,EANW,+BAA+B,KAA/B,+BAA+B,QAM1C;AAmBD,MAAM,UAAU,wBAAwB,CACvC,GAAQ;IAER,IAAM,IAAI,GAAqC,CAAC,UAAU,CAAC,CAAC;IAC5D,OAAO,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,UAAA,CAAC,IAAI,OAAA,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,EAArB,CAAqB,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,8BAA8B,CAC7C,GAAQ;IAER,IAAM,IAAI,GAA2C,CAAC,gBAAgB,CAAC,CAAC;IACxE,OAAO,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,UAAA,CAAC,IAAI,OAAA,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,EAArB,CAAqB,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GA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AA;IAC7B,+CAA6B,CAAA;IAC7B,qDAAmC,CAAA;AACpC,CAAC,EAhBW,cAAc,KAAd,cAAc,QAgBzB;AAkBD,MAAM,UAAU,sBAAsB,CAAC,GAAQ;IAC9C,OAAO,CAAC,CAAE,GAA4B,CAAC,QAAQ,CAAC;AACjD,CAAC;AAaD,MAAM,CAAN,IAAY,iBAMX;AAND,WAAY,iBAAiB;IAC5B,wCAAmB,CAAA;IACnB,wCAAmB,CAAA;IACnB,sDAAiC,CAAA;IACjC,4EAAuD,CAAA;IACvD,8CAAyB,CAAA;AAC1B,CAAC,EANW,iBAAiB,KAAjB,iBAAiB,QAM5B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/auth",
3
- "version": "4.5.11-unstable.2+366c32e2d",
3
+ "version": "4.5.11-unstable.3+e54617f28",
4
4
  "description": "Auth category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -41,9 +41,9 @@
41
41
  },
42
42
  "homepage": "https://aws-amplify.github.io/",
43
43
  "dependencies": {
44
- "@aws-amplify/cache": "4.0.49-unstable.2+366c32e2d",
45
- "@aws-amplify/core": "4.5.11-unstable.2+366c32e2d",
46
- "amazon-cognito-identity-js": "5.2.11-unstable.12+366c32e2d",
44
+ "@aws-amplify/cache": "4.0.49-unstable.3+e54617f28",
45
+ "@aws-amplify/core": "4.5.11-unstable.3+e54617f28",
46
+ "amazon-cognito-identity-js": "5.2.11-unstable.13+e54617f28",
47
47
  "crypto-js": "^4.1.1"
48
48
  },
49
49
  "devDependencies": {
@@ -95,5 +95,5 @@
95
95
  ],
96
96
  "testSequencer": "./testSequencer.js"
97
97
  },
98
- "gitHead": "366c32e2d87d73210bbd01ca1da55a5899f5a503"
98
+ "gitHead": "e54617f2878244f0e391d2d49f5cd2e8a8c069f9"
99
99
  }
package/src/Auth.ts CHANGED
@@ -44,6 +44,7 @@ import {
44
44
  browserOrNode,
45
45
  UniversalStorage,
46
46
  urlSafeDecode,
47
+ HubCallback,
47
48
  } from '@aws-amplify/core';
48
49
  import {
49
50
  CookieStorage,
@@ -70,6 +71,7 @@ import { default as urlListener } from './urlListener';
70
71
  import { AuthError, NoUserPoolError } from './Errors';
71
72
  import {
72
73
  AuthErrorTypes,
74
+ AutoSignInOptions,
73
75
  CognitoHostedUIIdentityProvider,
74
76
  IAuthDevice,
75
77
  } from './types/Auth';
@@ -95,6 +97,8 @@ const dispatchAuthEvent = (event: string, data: any, message: string) => {
95
97
  // https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListDevices.html#API_ListDevices_RequestSyntax
96
98
  const MAX_DEVICES = 60;
97
99
 
100
+ const MAX_AUTOSIGNIN_POLLING_MS = 3 * 60 * 1000;
101
+
98
102
  /**
99
103
  * Provide authentication steps
100
104
  */
@@ -107,6 +111,7 @@ export class AuthClass {
107
111
  private _storageSync;
108
112
  private oAuthFlowInProgress: boolean = false;
109
113
  private pendingSignIn: ReturnType<AuthClass['signInWithPassword']> | null;
114
+ private autoSignInInitiated: boolean = false;
110
115
 
111
116
  Credentials = Credentials;
112
117
 
@@ -257,6 +262,24 @@ export class AuthClass {
257
262
  null,
258
263
  `The Auth category has been configured successfully`
259
264
  );
265
+
266
+ if (
267
+ !this.autoSignInInitiated &&
268
+ typeof this._storage['getItem'] === 'function'
269
+ ) {
270
+ const pollingInitiated = this.isTrueStorageValue(
271
+ 'amplify-polling-started'
272
+ );
273
+ if (pollingInitiated) {
274
+ dispatchAuthEvent(
275
+ 'autoSignIn_failure',
276
+ null,
277
+ AuthErrorTypes.AutoSignInError
278
+ );
279
+ this._storage.removeItem('amplify-auto-sign-in');
280
+ }
281
+ this._storage.removeItem('amplify-polling-started');
282
+ }
260
283
  return this._config;
261
284
  }
262
285
 
@@ -295,6 +318,9 @@ export class AuthClass {
295
318
  const attributes: CognitoUserAttribute[] = [];
296
319
  let validationData: CognitoUserAttribute[] = null;
297
320
  let clientMetadata;
321
+ let autoSignIn: AutoSignInOptions = { enabled: false };
322
+ let autoSignInValidationData = {};
323
+ let autoSignInClientMetaData: ClientMetaData = {};
298
324
 
299
325
  if (params && typeof params === 'string') {
300
326
  username = params;
@@ -345,6 +371,13 @@ export class AuthClass {
345
371
  );
346
372
  });
347
373
  }
374
+
375
+ autoSignIn = params.autoSignIn ?? { enabled: false };
376
+ if (autoSignIn.enabled) {
377
+ this._storage.setItem('amplify-auto-sign-in', true);
378
+ autoSignInValidationData = autoSignIn.validationData ?? {};
379
+ autoSignInClientMetaData = autoSignIn.clientMetaData ?? {};
380
+ }
348
381
  } else {
349
382
  return this.rejectAuthError(AuthErrorTypes.SignUpError);
350
383
  }
@@ -379,6 +412,15 @@ export class AuthClass {
379
412
  data,
380
413
  `${username} has signed up successfully`
381
414
  );
415
+ if (autoSignIn.enabled) {
416
+ this.handleAutoSignIn(
417
+ username,
418
+ password,
419
+ autoSignInValidationData,
420
+ autoSignInClientMetaData,
421
+ data
422
+ );
423
+ }
382
424
  resolve(data);
383
425
  }
384
426
  },
@@ -387,6 +429,97 @@ export class AuthClass {
387
429
  });
388
430
  }
389
431
 
432
+ private handleAutoSignIn(
433
+ username: string,
434
+ password: string,
435
+ validationData: {},
436
+ clientMetadata: any,
437
+ data: any
438
+ ) {
439
+ this.autoSignInInitiated = true;
440
+ const authDetails = new AuthenticationDetails({
441
+ Username: username,
442
+ Password: password,
443
+ ValidationData: validationData,
444
+ ClientMetadata: clientMetadata,
445
+ });
446
+ if (data.userConfirmed) {
447
+ this.signInAfterUserConfirmed(authDetails);
448
+ } else if (this._config.signUpVerificationMethod === 'link') {
449
+ this.handleLinkAutoSignIn(authDetails);
450
+ } else {
451
+ this.handleCodeAutoSignIn(authDetails);
452
+ }
453
+ }
454
+
455
+ private handleCodeAutoSignIn(authDetails: AuthenticationDetails) {
456
+ const listenEvent = ({ payload }) => {
457
+ if (payload.event === 'confirmSignUp') {
458
+ this.signInAfterUserConfirmed(authDetails, listenEvent);
459
+ }
460
+ };
461
+ Hub.listen('auth', listenEvent);
462
+ }
463
+
464
+ private handleLinkAutoSignIn(authDetails: AuthenticationDetails) {
465
+ this._storage.setItem('amplify-polling-started', true);
466
+ const start = Date.now();
467
+ const autoSignInPollingIntervalId = setInterval(() => {
468
+ if (Date.now() - start > MAX_AUTOSIGNIN_POLLING_MS) {
469
+ clearInterval(autoSignInPollingIntervalId);
470
+ dispatchAuthEvent(
471
+ 'autoSignIn_failure',
472
+ null,
473
+ 'Please confirm your account and use your credentials to sign in.'
474
+ );
475
+ this._storage.removeItem('amplify-auto-sign-in');
476
+ } else {
477
+ this.signInAfterUserConfirmed(
478
+ authDetails,
479
+ null,
480
+ autoSignInPollingIntervalId
481
+ );
482
+ }
483
+ }, 5000);
484
+ }
485
+
486
+ private async signInAfterUserConfirmed(
487
+ authDetails: AuthenticationDetails,
488
+ listenEvent?: HubCallback,
489
+ autoSignInPollingIntervalId?: ReturnType<typeof setInterval>
490
+ ) {
491
+ const user = this.createCognitoUser(authDetails.getUsername());
492
+ try {
493
+ await user.authenticateUser(
494
+ authDetails,
495
+ this.authCallbacks(
496
+ user,
497
+ value => {
498
+ dispatchAuthEvent(
499
+ 'autoSignIn',
500
+ value,
501
+ `${authDetails.getUsername()} has signed in successfully`
502
+ );
503
+ if (listenEvent) {
504
+ Hub.remove('auth', listenEvent);
505
+ }
506
+ if (autoSignInPollingIntervalId) {
507
+ clearInterval(autoSignInPollingIntervalId);
508
+ this._storage.removeItem('amplify-polling-started');
509
+ }
510
+ this._storage.removeItem('amplify-auto-sign-in');
511
+ },
512
+ error => {
513
+ logger.error(error);
514
+ this._storage.removeItem('amplify-auto-sign-in');
515
+ }
516
+ )
517
+ );
518
+ } catch (error) {
519
+ logger.error(error);
520
+ }
521
+ }
522
+
390
523
  /**
391
524
  * Send the verification code to confirm sign up
392
525
  * @param {String} username - The username to be confirmed
@@ -429,6 +562,20 @@ export class AuthClass {
429
562
  if (err) {
430
563
  reject(err);
431
564
  } else {
565
+ dispatchAuthEvent(
566
+ 'confirmSignUp',
567
+ data,
568
+ `${username} has been confirmed successfully`
569
+ );
570
+ const autoSignIn = this.isTrueStorageValue('amplify-auto-sign-in');
571
+ if (autoSignIn && !this.autoSignInInitiated) {
572
+ dispatchAuthEvent(
573
+ 'autoSignIn_failure',
574
+ null,
575
+ AuthErrorTypes.AutoSignInError
576
+ );
577
+ this._storage.removeItem('amplify-auto-sign-in');
578
+ }
432
579
  resolve(data);
433
580
  }
434
581
  },
@@ -437,6 +584,11 @@ export class AuthClass {
437
584
  });
438
585
  }
439
586
 
587
+ private isTrueStorageValue(value: string) {
588
+ const item = this._storage.getItem(value);
589
+ return item ? item === 'true' : false;
590
+ }
591
+
440
592
  /**
441
593
  * Resend the verification code
442
594
  * @param {String} username - The username to be confirmed
package/src/Errors.ts CHANGED
@@ -110,6 +110,9 @@ export const authErrorMessages: AuthErrorMessages = {
110
110
  networkError: {
111
111
  message: AuthErrorStrings.NETWORK_ERROR,
112
112
  },
113
+ autoSignInError: {
114
+ message: AuthErrorStrings.AUTOSIGNIN_ERROR,
115
+ },
113
116
  default: {
114
117
  message: AuthErrorStrings.DEFAULT_MSG,
115
118
  },
@@ -13,4 +13,5 @@ export enum AuthErrorStrings {
13
13
  NO_USER_SESSION = 'Failed to get the session because the user is empty',
14
14
  NETWORK_ERROR = 'Network Error',
15
15
  DEVICE_CONFIG = 'Device tracking has not been configured in this User Pool',
16
+ AUTOSIGNIN_ERROR = 'Please use your credentials to sign in',
16
17
  }
package/src/types/Auth.ts CHANGED
@@ -25,6 +25,7 @@ export interface SignUpParams {
25
25
  attributes?: object;
26
26
  validationData?: { [key: string]: any };
27
27
  clientMetadata?: { [key: string]: string };
28
+ autoSignIn?: AutoSignInOptions;
28
29
  }
29
30
 
30
31
  export interface AuthCache {
@@ -50,6 +51,7 @@ export interface AuthOptions {
50
51
  identityPoolRegion?: string;
51
52
  clientMetadata?: any;
52
53
  endpoint?: string;
54
+ signUpVerificationMethod?: 'code' | 'link';
53
55
  }
54
56
 
55
57
  export enum CognitoHostedUIIdentityProvider {
@@ -201,6 +203,7 @@ export enum AuthErrorTypes {
201
203
  Default = 'default',
202
204
  DeviceConfig = 'deviceConfig',
203
205
  NetworkError = 'networkError',
206
+ AutoSignInError = 'autoSignInError',
204
207
  }
205
208
 
206
209
  export type AuthErrorMessages = { [key in AuthErrorTypes]: AuthErrorMessage };
@@ -228,6 +231,12 @@ export interface IAuthDevice {
228
231
  name: string;
229
232
  }
230
233
 
234
+ export interface AutoSignInOptions {
235
+ enabled: boolean;
236
+ clientMetaData?: ClientMetaData;
237
+ validationData?: { [key: string]: any };
238
+ }
239
+
231
240
  export enum GRAPHQL_AUTH_MODE {
232
241
  API_KEY = 'API_KEY',
233
242
  AWS_IAM = 'AWS_IAM',