diff --git a/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts b/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts index 46e4d4b558d..05514519641 100644 --- a/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts +++ b/packages/aws-amplify/__tests__/Auth/auth-unit-test.ts @@ -1378,11 +1378,6 @@ describe('auth unit test', () => { const spyon2 = jest.spyOn(Auth.prototype, 'userAttributes') .mockImplementationOnce(() => { - auth['credentials'] = new CognitoIdentityCredentials({ - IdentityPoolId: 'identityPoolId', - IdentityId: 'identityId' - }); - auth['credentials']['identityId'] = 'identityId'; return new Promise((res, rej)=> { res([ {Name: 'email', Value: 'email'}, @@ -1394,6 +1389,13 @@ describe('auth unit test', () => { }); }); + const spyon3 = jest.spyOn(Auth.prototype, 'currentCredentials').mockImplementationOnce(() => { + auth['credentials'] = { + identityId: 'identityId' + } + return Promise.resolve(); + }); + expect.assertions(1); expect(await auth.currentUserInfo()).toEqual({ username: 'username', diff --git a/packages/aws-amplify/src/Auth/Auth.ts b/packages/aws-amplify/src/Auth/Auth.ts index d3633745332..300b10cfeb3 100644 --- a/packages/aws-amplify/src/Auth/Auth.ts +++ b/packages/aws-amplify/src/Auth/Auth.ts @@ -977,8 +977,17 @@ export default class AuthClass { const attributes = await this.userAttributes(user); const userAttrs:object = this.attributesToObject(attributes); + // check if the credentials is in the memory + if (!this.credentials) { + try { + await this.currentCredentials(); + } catch (e) { + logger.debug('Failed to retrieve credentials while getting current user info', e); + } + } + const info = { - 'id': this.credentials.identityId, + 'id': this.credentials? this.credentials.identityId : undefined, 'username': user.username, 'attributes': userAttrs };