diff --git a/packages/auth/__tests__/providers/cognito/getCurrentUser.test.ts b/packages/auth/__tests__/providers/cognito/getCurrentUser.test.ts index c7cdfe014a0..1c7b3bc2920 100644 --- a/packages/auth/__tests__/providers/cognito/getCurrentUser.test.ts +++ b/packages/auth/__tests__/providers/cognito/getCurrentUser.test.ts @@ -2,18 +2,15 @@ // SPDX-License-Identifier: Apache-2.0 import { Amplify } from 'aws-amplify'; -import { decodeJWT, fetchAuthSession } from '@aws-amplify/core/internals/utils'; +import { decodeJWT } from '@aws-amplify/core/internals/utils'; import { AuthError } from '../../../src/errors/AuthError'; import { getCurrentUser } from '../../../src/providers/cognito'; import { InitiateAuthException } from '../../../src/providers/cognito/types/errors'; import { fetchTransferHandler } from '@aws-amplify/core/internals/aws-client-utils'; import { buildMockErrorResponse, mockJsonResponse } from './testUtils/data'; - +import { Amplify as AmplifyV6 } from '@aws-amplify/core'; +import { USER_UNAUTHENTICATED_EXCEPTION } from '../../../src/errors/constants'; jest.mock('@aws-amplify/core/lib/clients/handlers/fetch'); -jest.mock('@aws-amplify/core/internals/utils', () => ({ - ...jest.requireActual('@aws-amplify/core/internals/utils'), - fetchAuthSession: jest.fn(), -})); Amplify.configure({ Auth: { @@ -26,27 +23,25 @@ Amplify.configure({ }); const mockedAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; -const mockFetchAuthSession = fetchAuthSession as jest.Mock; +const mockGetTokensFunction = jest.spyOn(AmplifyV6.Auth, 'getTokens'); const mockedSub = 'mockedSub'; const mockedUsername = 'XXXXXXXXXXXXXX'; describe('getUser API happy path cases', () => { beforeEach(() => { - mockFetchAuthSession.mockResolvedValue({ - tokens: { - accessToken: decodeJWT(mockedAccessToken), - idToken: { - payload: { - sub: mockedSub, - 'cognito:username': mockedUsername, - }, + mockGetTokensFunction.mockResolvedValue({ + accessToken: decodeJWT(mockedAccessToken), + idToken: { + payload: { + sub: mockedSub, + 'cognito:username': mockedUsername, }, }, }); }); afterEach(() => { - mockFetchAuthSession.mockClear(); + mockGetTokensFunction.mockClear(); }); test('get current user', async () => { @@ -56,26 +51,20 @@ describe('getUser API happy path cases', () => { }); describe('getUser API error path cases:', () => { - test('getUser API should raise service error', async () => { - expect.assertions(2); - mockFetchAuthSession.mockImplementationOnce(async () => { - throw new AuthError({ - name: InitiateAuthException.InternalErrorException, - message: 'error at fetchAuthSession', - }); - }); - (fetchTransferHandler as jest.Mock).mockResolvedValue( - mockJsonResponse( - buildMockErrorResponse(InitiateAuthException.InternalErrorException) - ) - ); + beforeEach(() => { + mockGetTokensFunction.mockResolvedValue(null); + }); + + afterEach(() => { + mockGetTokensFunction.mockClear(); + }); + test('getUser API should raise a validation error when tokens are not found', async () => { try { - await getCurrentUser({ - recache: true, - }); + const result = await getCurrentUser(); } catch (error) { + console.log(error); expect(error).toBeInstanceOf(AuthError); - expect(error.name).toBe(InitiateAuthException.InternalErrorException); + expect(error.name).toBe(USER_UNAUTHENTICATED_EXCEPTION); } }); }); diff --git a/packages/auth/package.json b/packages/auth/package.json index 707e27a39c3..0234779df75 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -25,7 +25,7 @@ "clean:size": "rimraf dual-publish-tmp tmp*", "format": "echo \"Not implemented\"", "lint": "tslint '{src}/**/*.ts' && npm run ts-coverage", - "ts-coverage": "typescript-coverage-report -p ./tsconfig.json -t 91.19" + "ts-coverage": "typescript-coverage-report -p ./tsconfig.json -t 91.18" }, "typesVersions": { ">=3.8": { diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts index 3bc11f17206..39f628e6c4e 100644 --- a/packages/auth/src/index.ts +++ b/packages/auth/src/index.ts @@ -24,7 +24,6 @@ export { } from './providers/cognito'; export { - GetCurrentUserInput, ConfirmResetPasswordInput, ConfirmSignInInput, ConfirmSignUpInput, diff --git a/packages/auth/src/providers/cognito/apis/getCurrentUser.ts b/packages/auth/src/providers/cognito/apis/getCurrentUser.ts index 19fa2719096..db5a9c30235 100644 --- a/packages/auth/src/providers/cognito/apis/getCurrentUser.ts +++ b/packages/auth/src/providers/cognito/apis/getCurrentUser.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { Amplify } from '@aws-amplify/core'; -import { GetCurrentUserInput, GetCurrentUserOutput } from '../types'; +import { GetCurrentUserOutput } from '../types'; import { getCurrentUser as getCurrentUserInternal } from './internal/getCurrentUser'; /** @@ -13,8 +13,6 @@ import { getCurrentUser as getCurrentUserInternal } from './internal/getCurrentU * @throws - {@link InitiateAuthException} - Thrown when the service fails to refresh the tokens. * @throws AuthTokenConfigException - Thrown when the token provider config is invalid. */ -export const getCurrentUser = async ( - input?: GetCurrentUserInput -): Promise => { - return getCurrentUserInternal(Amplify, input); +export const getCurrentUser = async (): Promise => { + return getCurrentUserInternal(Amplify); }; diff --git a/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts b/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts index 093cc506dd1..a1304858911 100644 --- a/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts +++ b/packages/auth/src/providers/cognito/apis/internal/getCurrentUser.ts @@ -2,22 +2,16 @@ // SPDX-License-Identifier: Apache-2.0 import { AmplifyClassV6 } from '@aws-amplify/core'; -import { - assertTokenProviderConfig, - fetchAuthSession, -} from '@aws-amplify/core/internals/utils'; -import { GetCurrentUserInput, GetCurrentUserOutput } from '../../types'; +import { assertTokenProviderConfig } from '@aws-amplify/core/internals/utils'; import { assertAuthTokens } from '../../utils/types'; +import { GetCurrentUserOutput } from '../../types'; export const getCurrentUser = async ( - amplify: AmplifyClassV6, - input?: GetCurrentUserInput + amplify: AmplifyClassV6 ): Promise => { const authConfig = amplify.getConfig().Auth?.Cognito; assertTokenProviderConfig(authConfig); - const { tokens } = await fetchAuthSession(amplify, { - forceRefresh: input?.recache ?? false, - }); + const tokens = await amplify.Auth.getTokens({ forceRefresh: false }); assertAuthTokens(tokens); const { 'cognito:username': username, sub } = tokens.idToken?.payload ?? {}; diff --git a/packages/auth/src/providers/cognito/apis/server/getCurrentUser.ts b/packages/auth/src/providers/cognito/apis/server/getCurrentUser.ts index f59de229943..daa7d7081eb 100644 --- a/packages/auth/src/providers/cognito/apis/server/getCurrentUser.ts +++ b/packages/auth/src/providers/cognito/apis/server/getCurrentUser.ts @@ -5,23 +5,18 @@ import { AmplifyServer, getAmplifyServerContext, } from '@aws-amplify/core/internals/adapter-core'; -import { GetCurrentUserOutput, GetCurrentUserInput } from '../../types'; +import { GetCurrentUserOutput } from '../../types'; import { getCurrentUser as getCurrentUserInternal } from '../internal/getCurrentUser'; /** * Gets the current user from the idToken. * - * @param input - The GetCurrentUserInput object. * @returns GetCurrentUserOutput * @throws - {@link InitiateAuthException} - Thrown when the service fails to refresh the tokens. * @throws AuthTokenConfigException - Thrown when the token provider config is invalid. */ export const getCurrentUser = async ( - contextSpec: AmplifyServer.ContextSpec, - input?: GetCurrentUserInput + contextSpec: AmplifyServer.ContextSpec ): Promise => { - return getCurrentUserInternal( - getAmplifyServerContext(contextSpec).amplify, - input - ); + return getCurrentUserInternal(getAmplifyServerContext(contextSpec).amplify); }; diff --git a/packages/auth/src/providers/cognito/index.ts b/packages/auth/src/providers/cognito/index.ts index fd05b4c7075..0f8fb9ffb24 100644 --- a/packages/auth/src/providers/cognito/index.ts +++ b/packages/auth/src/providers/cognito/index.ts @@ -20,7 +20,6 @@ export { signInWithRedirect } from './apis/signInWithRedirect'; export { fetchUserAttributes } from './apis/fetchUserAttributes'; export { signOut } from './apis/signOut'; export { - GetCurrentUserInput, ConfirmResetPasswordInput, ConfirmSignInInput, ConfirmSignUpInput, diff --git a/packages/auth/src/providers/cognito/types/index.ts b/packages/auth/src/providers/cognito/types/index.ts index 15aa1cb5d8b..c24963b7098 100644 --- a/packages/auth/src/providers/cognito/types/index.ts +++ b/packages/auth/src/providers/cognito/types/index.ts @@ -22,7 +22,6 @@ export { } from './options'; export { - GetCurrentUserInput, ConfirmResetPasswordInput, ConfirmSignInInput, ConfirmSignUpInput, diff --git a/packages/auth/src/providers/cognito/types/inputs.ts b/packages/auth/src/providers/cognito/types/inputs.ts index be22e8bbcec..7f9ae8d4e26 100644 --- a/packages/auth/src/providers/cognito/types/inputs.ts +++ b/packages/auth/src/providers/cognito/types/inputs.ts @@ -15,7 +15,6 @@ import { VerifyTOTPSetupOptions, } from '../types'; import { - AuthGetCurrentUserInput, AuthConfirmResetPasswordInput, AuthConfirmSignInInput, AuthConfirmSignUpInput, @@ -31,11 +30,6 @@ import { AuthVerifyTOTPSetupInput, } from '../../../types'; -/** - * Input type for Cognito getCurrentUser API. - */ -export type GetCurrentUserInput = AuthGetCurrentUserInput; - /** * Input type for Cognito confirmResetPassword API. */ diff --git a/packages/auth/src/types/index.ts b/packages/auth/src/types/index.ts index 171e4ad3e90..25d923ce952 100644 --- a/packages/auth/src/types/index.ts +++ b/packages/auth/src/types/index.ts @@ -34,7 +34,6 @@ export { AuthConfirmSignInInput, AuthUpdatePasswordInput, AuthUpdateUserAttributesInput, - AuthGetCurrentUserInput, AuthConfirmUserAttributeInput, AuthVerifyTOTPSetupInput, AuthSignInWithRedirectInput, diff --git a/packages/auth/src/types/inputs.ts b/packages/auth/src/types/inputs.ts index d94968dafb3..912f5c90129 100644 --- a/packages/auth/src/types/inputs.ts +++ b/packages/auth/src/types/inputs.ts @@ -137,12 +137,6 @@ export type AuthUpdateUserAttributesInput< options?: { serviceOptions?: ServiceOptions }; }; -/** - * Constructs a `GetCurrentUser` input. - * @param recache - whether to recache the user - */ -export type AuthGetCurrentUserInput = { recache: boolean }; - /* * Constructs a `verifyUserAttribute` input. * diff --git a/packages/core/src/singleton/Auth/index.ts b/packages/core/src/singleton/Auth/index.ts index 8c84e6b89cb..66321ebb7d6 100644 --- a/packages/core/src/singleton/Auth/index.ts +++ b/packages/core/src/singleton/Auth/index.ts @@ -52,8 +52,7 @@ export class AuthClass { let userSub: string | undefined; // Get tokens will throw if session cannot be refreshed (network or service error) or return null if not available - tokens = - (await this.authOptions?.tokenProvider?.getTokens(options)) ?? undefined; + tokens = await this.getTokens(options); if (tokens) { userSub = tokens.accessToken?.payload?.sub; @@ -93,4 +92,12 @@ export class AuthClass { return await this.authOptions.credentialsProvider.clearCredentialsAndIdentityId(); } } + + async getTokens( + options: FetchAuthSessionOptions + ): Promise { + return ( + (await this.authOptions?.tokenProvider?.getTokens(options)) ?? undefined + ); + } }