Skip to content

Commit

Permalink
fix(auth): sends current user with attributes to hub (#10947)
Browse files Browse the repository at this point in the history
  • Loading branch information
haverchuck authored Feb 21, 2023
1 parent bb505bb commit 4a9a71d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
66 changes: 64 additions & 2 deletions packages/auth/__tests__/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jest.mock('amazon-cognito-identity-js/lib/CognitoUserPool', () => {
CognitoUserPool.prototype.getCurrentUser = () => {
return {
username: 'username',
attributes: { email: '[email protected]' },
getSession: callback => {
// throw 3;
callback(null, {
Expand Down Expand Up @@ -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: '[email protected]' });
const result = await auth.confirmSignIn(user, 'code', null);
expect(result.attributes.email).toEqual('[email protected]');
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);
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -3031,7 +3093,7 @@ describe('auth unit test', () => {
} catch (e) {
expect(e).toEqual(new Error('Error'));
}

spyon.mockClear();
});

Expand All @@ -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({
Expand Down
12 changes: 8 additions & 4 deletions packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -1447,8 +1451,8 @@ export class AuthClass {
}

private createUpdateAttributesResultList(
attributes: Record<string, string>,
codeDeliveryDetailsList?: CodeDeliveryDetails []
attributes: Record<string, string>,
codeDeliveryDetailsList?: CodeDeliveryDetails[]
): Record<string, string> {
const attrs = {};
Object.keys(attributes).forEach(key => {
Expand Down

0 comments on commit 4a9a71d

Please sign in to comment.