From 4a9a71d2299ff70591040cb481ec9e574ece5a60 Mon Sep 17 00:00:00 2001 From: Dustin Noyes Date: Tue, 21 Feb 2023 09:05:06 -0800 Subject: [PATCH] fix(auth): sends current user with attributes to hub (#10947) --- packages/auth/__tests__/auth-unit-test.ts | 66 ++++++++++++++++++++++- packages/auth/src/Auth.ts | 12 +++-- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/packages/auth/__tests__/auth-unit-test.ts b/packages/auth/__tests__/auth-unit-test.ts index 3bf64da2e36..ecafed17013 100644 --- a/packages/auth/__tests__/auth-unit-test.ts +++ b/packages/auth/__tests__/auth-unit-test.ts @@ -80,6 +80,7 @@ jest.mock('amazon-cognito-identity-js/lib/CognitoUserPool', () => { CognitoUserPool.prototype.getCurrentUser = () => { return { username: 'username', + attributes: { email: 'test@test.com' }, getSession: callback => { // throw 3; callback(null, { @@ -1332,6 +1333,34 @@ describe('auth unit test', () => { spyon.mockClear(); }); + test('happy case attributes are appended', async () => { + const spyon = jest + .spyOn(CognitoUser.prototype, 'sendMFACode') + .mockImplementationOnce((code, callback) => { + callback.onSuccess(session); + }); + const hubSpy = jest.spyOn(Hub, 'dispatch'); + const auth = new Auth(authOptions); + const user = new CognitoUser({ + Username: 'username', + Pool: userPool, + }); + const expectedUser = Object.assign(user, { email: 'test@test.com' }); + const result = await auth.confirmSignIn(user, 'code', null); + expect(result.attributes.email).toEqual('test@test.com'); + expect(hubSpy).toHaveBeenCalledWith( + 'auth', + { + data: expectedUser, + event: 'signIn', + message: 'A user username has been signed in', + }, + 'Auth', + Symbol.for('amplify_default') + ); + spyon.mockClear(); + }); + test('happy case clientMetadata default', async () => { const spyon = jest.spyOn(CognitoUser.prototype, 'sendMFACode'); const auth = new Auth(authOptionsWithClientMetadata); @@ -1378,6 +1407,39 @@ describe('auth unit test', () => { spyon.mockClear(); }); + test('currentUserPoolUser fails but hub event still dispatches', async () => { + const auth = new Auth(authOptions); + const spyon = jest + .spyOn(CognitoUser.prototype, 'sendMFACode') + .mockImplementationOnce((code, callback) => { + callback.onSuccess(session); + }); + + const spyon2 = jest + .spyOn(auth, 'currentUserPoolUser') + .mockImplementationOnce(() => { + return Promise.reject('Could not get current user.'); + }); + const hubSpy = jest.spyOn(Hub, 'dispatch'); + const user = new CognitoUser({ + Username: 'username', + Pool: userPool, + }); + const result = await auth.confirmSignIn(user, 'code', null); + expect(result).toEqual(user); + expect(hubSpy).toHaveBeenCalledWith( + 'auth', + { + data: user, + event: 'signIn', + message: 'A user username has been signed in', + }, + 'Auth', + Symbol.for('amplify_default') + ); + spyon.mockClear(); + }); + test('onFailure', async () => { const spyon = jest .spyOn(CognitoUser.prototype, 'sendMFACode') @@ -3031,7 +3093,7 @@ describe('auth unit test', () => { } catch (e) { expect(e).toEqual(new Error('Error')); } - + spyon.mockClear(); }); @@ -3050,7 +3112,7 @@ describe('auth unit test', () => { const spyon = jest.spyOn(CognitoUser.prototype, 'updateAttributes') .mockImplementationOnce((attrs, callback: any) => { callback(null, 'SUCCESS', codeDeliverDetailsResult); - }); + }); const auth = new Auth(authOptions); const user = new CognitoUser({ diff --git a/packages/auth/src/Auth.ts b/packages/auth/src/Auth.ts index 0ab4efc7a91..ef347a820bc 100644 --- a/packages/auth/src/Auth.ts +++ b/packages/auth/src/Auth.ts @@ -1183,7 +1183,12 @@ export class AuthClass { logger.debug('cannot get cognito credentials', e); } finally { that.user = user; - + try { + const currentUser = await this.currentUserPoolUser(); + user.attributes = currentUser.attributes; + } catch (e) { + logger.debug('cannot get updated Cognito User', e); + } dispatchAuthEvent( 'signIn', user, @@ -1428,7 +1433,6 @@ export class AuthClass { user.updateAttributes( attributeList, (err, result, details) => { - if (err) { dispatchAuthEvent('updateUserAttributes_failure', err, 'Failed to update attributes'); return reject(err); @@ -1447,8 +1451,8 @@ export class AuthClass { } private createUpdateAttributesResultList( - attributes: Record, - codeDeliveryDetailsList?: CodeDeliveryDetails [] + attributes: Record, + codeDeliveryDetailsList?: CodeDeliveryDetails[] ): Record { const attrs = {}; Object.keys(attributes).forEach(key => {