From 4d283b0ae822551f29273e897ec5a752d32738db Mon Sep 17 00:00:00 2001 From: Hui Zhao Date: Mon, 23 Dec 2024 15:26:21 -0800 Subject: [PATCH] chore(adapter-nextjs): rename hasUserSignedIn to hasActiveUserSession --- ...ndleAuthApiRouteRequestForAppRouter.test.ts | 4 ++-- ...leAuthApiRouteRequestForPagesRouter.test.ts | 4 ++-- ...In.test.ts => hasActiveUserSession.test.ts} | 18 +++++++++--------- .../handleAuthApiRouteRequestForAppRouter.ts | 10 +++++----- .../handleAuthApiRouteRequestForPagesRouter.ts | 10 +++++----- ...UserSignedIn.ts => hasActiveUserSession.ts} | 4 ++-- .../adapter-nextjs/src/auth/utils/index.ts | 6 +++--- 7 files changed, 28 insertions(+), 28 deletions(-) rename packages/adapter-nextjs/__tests__/auth/utils/{hasUserSignedIn.test.ts => hasActiveUserSession.test.ts} (88%) rename packages/adapter-nextjs/src/auth/utils/{hasUserSignedIn.ts => hasActiveUserSession.ts} (91%) diff --git a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts index a21143a965b..ccb48a358bf 100644 --- a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts @@ -14,7 +14,7 @@ import { } from '../../src/auth/handlers'; import { NextServer } from '../../src'; import { - hasUserSignedInWithAppRouter, + hasActiveUserSessionWithAppRouter, isSupportedAuthApiRoutePath, } from '../../src/auth/utils'; @@ -30,7 +30,7 @@ const mockHandleSignOutCallbackRequest = jest.mocked( handleSignOutCallbackRequest, ); const mockHasUserSignedInWithAppRouter = jest.mocked( - hasUserSignedInWithAppRouter, + hasActiveUserSessionWithAppRouter, ); const mockIsSupportedAuthApiRoutePath = jest.mocked( isSupportedAuthApiRoutePath, diff --git a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts index 1e6603f71f5..bc701c92c07 100644 --- a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts @@ -11,7 +11,7 @@ import { } from '../../src/auth/handlers'; import { NextServer } from '../../src'; import { - hasUserSignedInWithPagesRouter, + hasActiveUserSessionWithPagesRouter, isSupportedAuthApiRoutePath, } from '../../src/auth/utils'; @@ -36,7 +36,7 @@ const mockIsSupportedAuthApiRoutePath = jest.mocked( isSupportedAuthApiRoutePath, ); const mockHasUserSignedInWithPagesRouter = jest.mocked( - hasUserSignedInWithPagesRouter, + hasActiveUserSessionWithPagesRouter, ); const mockRunWithAmplifyServerContext = jest.fn() as jest.MockedFunction; diff --git a/packages/adapter-nextjs/__tests__/auth/utils/hasUserSignedIn.test.ts b/packages/adapter-nextjs/__tests__/auth/utils/hasActiveUserSession.test.ts similarity index 88% rename from packages/adapter-nextjs/__tests__/auth/utils/hasUserSignedIn.test.ts rename to packages/adapter-nextjs/__tests__/auth/utils/hasActiveUserSession.test.ts index e1e6dd32575..23799e1dcf2 100644 --- a/packages/adapter-nextjs/__tests__/auth/utils/hasUserSignedIn.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/utils/hasActiveUserSession.test.ts @@ -7,9 +7,9 @@ import { AuthUser } from '@aws-amplify/auth/cognito'; import { NextApiRequest } from 'next'; import { - hasUserSignedInWithAppRouter, - hasUserSignedInWithPagesRouter, -} from '../../../src/auth/utils/hasUserSignedIn'; + hasActiveUserSessionWithAppRouter, + hasActiveUserSessionWithPagesRouter, +} from '../../../src/auth/utils/hasActiveUserSession'; import { NextServer } from '../../../src/types'; import { createMockNextApiResponse } from '../testUtils'; @@ -44,7 +44,7 @@ describe('hasUserSignedIn', () => { const mockRequest = new NextRequest('https://example.com/api/auth/sign-in'); it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => { - await hasUserSignedInWithAppRouter({ + await hasActiveUserSessionWithAppRouter({ request: mockRequest, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, }); @@ -60,7 +60,7 @@ describe('hasUserSignedIn', () => { }); it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => { - const result = await hasUserSignedInWithAppRouter({ + const result = await hasActiveUserSessionWithAppRouter({ request: mockRequest, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, }); @@ -71,7 +71,7 @@ describe('hasUserSignedIn', () => { it('returns false when getCurrentUser() rejects (no auth tokens)', async () => { mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user')); - const result = await hasUserSignedInWithAppRouter({ + const result = await hasActiveUserSessionWithAppRouter({ request: mockRequest, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, }); @@ -87,7 +87,7 @@ describe('hasUserSignedIn', () => { const { mockResponse } = createMockNextApiResponse(); it('invokes server getCurrentUser() with expected parameter within the injected runWithAmplifyServerContext function', async () => { - await hasUserSignedInWithPagesRouter({ + await hasActiveUserSessionWithPagesRouter({ request: mockRequest, response: mockResponse, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, @@ -104,7 +104,7 @@ describe('hasUserSignedIn', () => { }); it('returns true when getCurrentUser() resolves (returned auth tokens)', async () => { - const result = await hasUserSignedInWithPagesRouter({ + const result = await hasActiveUserSessionWithPagesRouter({ request: mockRequest, response: mockResponse, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, @@ -116,7 +116,7 @@ describe('hasUserSignedIn', () => { it('returns false when getCurrentUser() rejects (no auth tokens)', async () => { mockGetCurrentUser.mockRejectedValueOnce(new Error('No current user')); - const result = await hasUserSignedInWithPagesRouter({ + const result = await hasActiveUserSessionWithPagesRouter({ request: mockRequest, response: mockResponse, runWithAmplifyServerContext: mockRunWithAmplifyServerContext, diff --git a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts index a37371a020a..ebcf44e7b1a 100644 --- a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts +++ b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts @@ -3,7 +3,7 @@ import { HandleAuthApiRouteRequestForAppRouter } from './types'; import { - hasUserSignedInWithAppRouter, + hasActiveUserSessionWithAppRouter, isSupportedAuthApiRoutePath, } from './utils'; import { @@ -40,12 +40,12 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor switch (slug) { case 'sign-up': { - const hasUserSignedIn = await hasUserSignedInWithAppRouter({ + const hasActiveUserSession = await hasActiveUserSessionWithAppRouter({ request, runWithAmplifyServerContext, }); - if (hasUserSignedIn) { + if (hasActiveUserSession) { return new Response(null, { status: 302, headers: new Headers({ @@ -65,12 +65,12 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor }); } case 'sign-in': { - const hasUserSignedIn = await hasUserSignedInWithAppRouter({ + const hasActiveUserSession = await hasActiveUserSessionWithAppRouter({ request, runWithAmplifyServerContext, }); - if (hasUserSignedIn) { + if (hasActiveUserSession) { return new Response(null, { status: 302, headers: new Headers({ diff --git a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts index 4bb3a3db7b2..2a80a8b5ab0 100644 --- a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts +++ b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts @@ -3,7 +3,7 @@ import { HandleAuthApiRouteRequestForPagesRouter } from './types'; import { - hasUserSignedInWithPagesRouter, + hasActiveUserSessionWithPagesRouter, isSupportedAuthApiRoutePath, } from './utils'; import { @@ -46,13 +46,13 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF switch (slug) { case 'sign-up': { - const hasUserSignedIn = await hasUserSignedInWithPagesRouter({ + const hasActiveUserSession = await hasActiveUserSessionWithPagesRouter({ request, response, runWithAmplifyServerContext, }); - if (hasUserSignedIn) { + if (hasActiveUserSession) { response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/'); return; @@ -71,13 +71,13 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF break; } case 'sign-in': { - const hasUserSignedIn = await hasUserSignedInWithPagesRouter({ + const hasActiveUserSession = await hasActiveUserSessionWithPagesRouter({ request, response, runWithAmplifyServerContext, }); - if (hasUserSignedIn) { + if (hasActiveUserSession) { response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/'); return; diff --git a/packages/adapter-nextjs/src/auth/utils/hasUserSignedIn.ts b/packages/adapter-nextjs/src/auth/utils/hasActiveUserSession.ts similarity index 91% rename from packages/adapter-nextjs/src/auth/utils/hasUserSignedIn.ts rename to packages/adapter-nextjs/src/auth/utils/hasActiveUserSession.ts index f3f7b3fe377..b9e6bd0f8cf 100644 --- a/packages/adapter-nextjs/src/auth/utils/hasUserSignedIn.ts +++ b/packages/adapter-nextjs/src/auth/utils/hasActiveUserSession.ts @@ -7,7 +7,7 @@ import { NextApiRequest, NextApiResponse } from 'next'; import { NextServer } from '../../types'; -export const hasUserSignedInWithAppRouter = async ({ +export const hasActiveUserSessionWithAppRouter = async ({ request, runWithAmplifyServerContext, }: { @@ -31,7 +31,7 @@ export const hasUserSignedInWithAppRouter = async ({ } }; -export const hasUserSignedInWithPagesRouter = async ({ +export const hasActiveUserSessionWithPagesRouter = async ({ request, response, runWithAmplifyServerContext, diff --git a/packages/adapter-nextjs/src/auth/utils/index.ts b/packages/adapter-nextjs/src/auth/utils/index.ts index 7d98d534a74..ffff7b38103 100644 --- a/packages/adapter-nextjs/src/auth/utils/index.ts +++ b/packages/adapter-nextjs/src/auth/utils/index.ts @@ -30,9 +30,9 @@ export { isNextRequest, } from './predicates'; export { - hasUserSignedInWithAppRouter, - hasUserSignedInWithPagesRouter, -} from './hasUserSignedIn'; + hasActiveUserSessionWithAppRouter, + hasActiveUserSessionWithPagesRouter, +} from './hasActiveUserSession'; export { isSupportedAuthApiRoutePath } from './isSupportedAuthApiRoutePath'; export { resolveCodeAndStateFromUrl } from './resolveCodeAndStateFromUrl'; export { resolveIdentityProviderFromUrl } from './resolveIdentityProviderFromUrl';