@aws-amplify/auth 4.3.21-unstable.9 → 4.4.0

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.
package/src/Auth.ts CHANGED
@@ -80,10 +80,11 @@ const USER_ADMIN_SCOPE = 'aws.cognito.signin.user.admin';
80
80
  // 10 sec, following this guide https://www.nngroup.com/articles/response-times-3-important-limits/
81
81
  const OAUTH_FLOW_MS_TIMEOUT = 10 * 1000;
82
82
 
83
- const AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
84
- typeof Symbol.for === 'function'
85
- ? Symbol.for('amplify_default')
86
- : '@@amplify_default') as Symbol;
83
+ const AMPLIFY_SYMBOL = (
84
+ typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
85
+ ? Symbol.for('amplify_default')
86
+ : '@@amplify_default'
87
+ ) as Symbol;
87
88
 
88
89
  const dispatchAuthEvent = (event: string, data: any, message: string) => {
89
90
  Hub.dispatch('auth', { event, data, message }, 'Auth', AMPLIFY_SYMBOL);
@@ -773,7 +774,7 @@ export class AuthClass {
773
774
  */
774
775
  public async setPreferredMFA(
775
776
  user: CognitoUser | any,
776
- mfaMethod: 'TOTP' | 'SMS' | 'NOMFA' | 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA'
777
+ mfaMethod: 'TOTP' | 'SMS' | 'NOMFA' | 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA'
777
778
  ): Promise<string> {
778
779
  const clientMetadata = this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
779
780
 
@@ -793,7 +794,7 @@ export class AuthClass {
793
794
  };
794
795
  break;
795
796
  case 'SMS':
796
- case 'SMS_MFA':
797
+ case 'SMS_MFA':
797
798
  smsMfaSettings = {
798
799
  PreferredMfa: true,
799
800
  Enabled: true,
@@ -1120,24 +1121,90 @@ export class AuthClass {
1120
1121
  **/
1121
1122
  public deleteUserAttributes(
1122
1123
  user: CognitoUser | any,
1123
- attributeNames: string[],
1124
+ attributeNames: string[]
1124
1125
  ) {
1125
1126
  const that = this;
1126
1127
  return new Promise((resolve, reject) => {
1127
1128
  that.userSession(user).then(session => {
1128
- user.deleteAttributes(
1129
- attributeNames,
1130
- (err, result) => {
1131
- if (err) {
1132
- return reject(err);
1133
- } else {
1134
- return resolve(result);
1135
- }
1129
+ user.deleteAttributes(attributeNames, (err, result) => {
1130
+ if (err) {
1131
+ return reject(err);
1132
+ } else {
1133
+ return resolve(result);
1136
1134
  }
1137
- );
1135
+ });
1138
1136
  });
1139
1137
  });
1138
+ }
1140
1139
 
1140
+ /**
1141
+ * Delete the current authenticated user
1142
+ * @return {Promise}
1143
+ **/
1144
+ //TODO: Check return type void
1145
+ public async deleteUser(): Promise<string | void> {
1146
+ try {
1147
+ await this._storageSync;
1148
+ } catch (e) {
1149
+ logger.debug('Failed to sync cache info into memory', e);
1150
+ throw new Error(e);
1151
+ }
1152
+
1153
+ const isSignedInHostedUI =
1154
+ this._oAuthHandler &&
1155
+ this._storage.getItem('amplify-signin-with-hostedUI') === 'true';
1156
+
1157
+ return new Promise(async (res, rej) => {
1158
+ if (this.userPool) {
1159
+ const user = this.userPool.getCurrentUser();
1160
+
1161
+ if (!user) {
1162
+ logger.debug('Failed to get user from user pool');
1163
+ return rej(new Error('No current user.'));
1164
+ } else {
1165
+ user.getSession(async (err, session) => {
1166
+ if (err) {
1167
+ logger.debug('Failed to get the user session', err);
1168
+ return rej(err);
1169
+ } else {
1170
+ user.deleteUser((err, result: string) => {
1171
+ if (err) {
1172
+ rej(err);
1173
+ } else {
1174
+ dispatchAuthEvent(
1175
+ 'userDeleted',
1176
+ result,
1177
+ 'The authenticated user has been deleted.'
1178
+ );
1179
+ user.signOut();
1180
+ this.user = null;
1181
+ try {
1182
+ this.cleanCachedItems(); // clean aws credentials
1183
+ } catch (e) {
1184
+ // TODO: change to rejects in refactor
1185
+ logger.debug('failed to clear cached items');
1186
+ }
1187
+
1188
+ if (isSignedInHostedUI) {
1189
+ this.oAuthSignOutRedirect(res, rej);
1190
+ } else {
1191
+ dispatchAuthEvent(
1192
+ 'signOut',
1193
+ this.user,
1194
+ `A user has been signed out`
1195
+ );
1196
+ res(result);
1197
+ }
1198
+ }
1199
+ });
1200
+ }
1201
+ });
1202
+ }
1203
+ } else {
1204
+ logger.debug('no Congito User pool');
1205
+ rej(new Error('Cognito User pool does not exist'));
1206
+ }
1207
+ });
1141
1208
  }
1142
1209
 
1143
1210
  /**
@@ -1841,7 +1908,7 @@ export class AuthClass {
1841
1908
  code,
1842
1909
  password,
1843
1910
  {
1844
- onSuccess: (success) => {
1911
+ onSuccess: success => {
1845
1912
  dispatchAuthEvent(
1846
1913
  'forgotPasswordSubmit',
1847
1914
  user,
@@ -2053,12 +2120,8 @@ export class AuthClass {
2053
2120
  if (hasCodeOrError || hasTokenOrError) {
2054
2121
  this._storage.setItem('amplify-redirected-from-hosted-ui', 'true');
2055
2122
  try {
2056
- const {
2057
- accessToken,
2058
- idToken,
2059
- refreshToken,
2060
- state,
2061
- } = await this._oAuthHandler.handleAuthResponse(currentUrl);
2123
+ const { accessToken, idToken, refreshToken, state } =
2124
+ await this._oAuthHandler.handleAuthResponse(currentUrl);
2062
2125
  const session = new CognitoUserSession({
2063
2126
  IdToken: new CognitoIdToken({ IdToken: idToken }),
2064
2127
  RefreshToken: new CognitoRefreshToken({
@@ -2115,10 +2178,7 @@ export class AuthClass {
2115
2178
  );
2116
2179
 
2117
2180
  if (isCustomStateIncluded) {
2118
- const customState = state
2119
- .split('-')
2120
- .splice(1)
2121
- .join('-');
2181
+ const customState = state.split('-').splice(1).join('-');
2122
2182
 
2123
2183
  dispatchAuthEvent(
2124
2184
  'customOAuthState',
@@ -2352,4 +2412,3 @@ export class AuthClass {
2352
2412
  export const Auth = new AuthClass(null);
2353
2413
 
2354
2414
  Amplify.register(Auth);
2355
-