From f8d978b7b488c00a00862af4efe9176df6287c05 Mon Sep 17 00:00:00 2001 From: Hui Zhao <10602282+HuiSF@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:37:04 -0800 Subject: [PATCH] fix(adapter-nextjs): wrong use of nullish coalescing (#14112) --- ...handleAuthApiRouteRequestForAppRouter.test.ts | 10 ++++++++++ ...ndleAuthApiRouteRequestForPagesRouter.test.ts | 7 +++++++ .../handlers/handleSignInCallbackRequest.test.ts | 11 +++++++++++ ...leSignInCallbackRequestForPagesRouter.test.ts | 12 ++++++++++++ .../handleSignOutCallbackRequest.test.ts | 13 +++++++++++++ ...eSignOutCallbackRequestForPagesRouter.test.ts | 13 +++++++++++++ .../auth/utils/cognitoHostedUIEndpoints.test.ts | 1 - .../auth/utils/getRedirectOrDefault.test.ts | 11 +++++++++++ .../handleAuthApiRouteRequestForAppRouter.ts | 9 +++++++-- .../handleAuthApiRouteRequestForPagesRouter.ts | 11 +++++++++-- .../auth/handlers/handleSignInCallbackRequest.ts | 5 ++++- .../handleSignInCallbackRequestForPagesRouter.ts | 6 ++++-- .../handlers/handleSignOutCallbackRequest.ts | 14 +++++++++++--- ...handleSignOutCallbackRequestForPagesRouter.ts | 16 +++++++++++++--- .../src/auth/utils/getRedirectOrDefault.ts | 5 +++++ packages/adapter-nextjs/src/auth/utils/index.ts | 1 + 16 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 packages/adapter-nextjs/__tests__/auth/utils/getRedirectOrDefault.test.ts create mode 100644 packages/adapter-nextjs/src/auth/utils/getRedirectOrDefault.ts diff --git a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts index ccb48a358bf..075dba13cad 100644 --- a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForAppRouter.test.ts @@ -14,6 +14,7 @@ import { } from '../../src/auth/handlers'; import { NextServer } from '../../src'; import { + getRedirectOrDefault, hasActiveUserSessionWithAppRouter, isSupportedAuthApiRoutePath, } from '../../src/auth/utils'; @@ -37,6 +38,7 @@ const mockIsSupportedAuthApiRoutePath = jest.mocked( ); const mockRunWithAmplifyServerContext = jest.fn() as jest.MockedFunction; +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleAuthApiRouteRequestForAppRouter', () => { const testOrigin = 'https://example.com'; @@ -57,6 +59,13 @@ describe('handleAuthApiRouteRequestForAppRouter', () => { beforeAll(() => { mockHasUserSignedInWithAppRouter.mockResolvedValue(false); mockIsSupportedAuthApiRoutePath.mockReturnValue(true); + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); + }); + + afterEach(() => { + mockGetRedirectOrDefault.mockClear(); }); it('returns a 405 response when input.request has an unsupported method', async () => { @@ -192,6 +201,7 @@ describe('handleAuthApiRouteRequestForAppRouter', () => { expect(response.status).toBe(302); expect(response.headers.get('Location')).toBe('/'); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith(undefined); }, ); diff --git a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts index bc701c92c07..a3590c252c8 100644 --- a/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handleAuthApiRouteRequestForPagesRouter.test.ts @@ -11,6 +11,7 @@ import { } from '../../src/auth/handlers'; import { NextServer } from '../../src'; import { + getRedirectOrDefault, hasActiveUserSessionWithPagesRouter, isSupportedAuthApiRoutePath, } from '../../src/auth/utils'; @@ -40,6 +41,7 @@ const mockHasUserSignedInWithPagesRouter = jest.mocked( ); const mockRunWithAmplifyServerContext = jest.fn() as jest.MockedFunction; +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleAuthApiRouteRequestForPagesRouter', () => { const testOrigin = 'https://example.com'; @@ -67,6 +69,9 @@ describe('handleAuthApiRouteRequestForPagesRouter', () => { beforeAll(() => { mockHasUserSignedInWithPagesRouter.mockResolvedValue(false); mockIsSupportedAuthApiRoutePath.mockReturnValue(true); + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); }); afterEach(() => { @@ -75,6 +80,7 @@ describe('handleAuthApiRouteRequestForPagesRouter', () => { mockResponseStatus.mockClear(); mockResponseSend.mockClear(); mockResponseRedirect.mockClear(); + mockGetRedirectOrDefault.mockClear(); }); it('sets response.status(405) when request has an unsupported method', () => { @@ -197,6 +203,7 @@ describe('handleAuthApiRouteRequestForPagesRouter', () => { }); expect(mockResponseRedirect).toHaveBeenCalledWith(302, '/'); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith(undefined); }, ); diff --git a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequest.test.ts b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequest.test.ts index 92290010bf5..ef3ee357a0a 100644 --- a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequest.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequest.test.ts @@ -15,6 +15,7 @@ import { createTokenCookiesSetOptions, exchangeAuthNTokens, getCookieValuesFromRequest, + getRedirectOrDefault, resolveCodeAndStateFromUrl, resolveRedirectSignInUrl, } from '../../../src/auth/utils'; @@ -44,6 +45,7 @@ const mockExchangeAuthNTokens = jest.mocked(exchangeAuthNTokens); const mockGetCookieValuesFromRequest = jest.mocked(getCookieValuesFromRequest); const mockResolveCodeAndStateFromUrl = jest.mocked(resolveCodeAndStateFromUrl); const mockResolveRedirectSignInUrl = jest.mocked(resolveRedirectSignInUrl); +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleSignInCallbackRequest', () => { const mockHandlerInput: CreateAuthRoutesHandlersInput = { @@ -55,6 +57,12 @@ describe('handleSignInCallbackRequest', () => { const mockSetCookieOptions = {} as CookieStorage.SetCookieOptions; const mockOrigin = 'https://example.com'; + beforeAll(() => { + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); + }); + afterEach(() => { mockAppendSetCookieHeaders.mockClear(); mockCreateAuthFlowProofCookiesRemoveOptions.mockClear(); @@ -307,6 +315,9 @@ describe('handleSignInCallbackRequest', () => { ).toHaveBeenCalledWith({ redirectOnSignInComplete: expectedFinalRedirect, }); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith( + handlerInput.redirectOnSignInComplete, + ); }, ); }); diff --git a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequestForPagesRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequestForPagesRouter.test.ts index 567668ab1d8..5fd5612ae0c 100644 --- a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequestForPagesRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignInCallbackRequestForPagesRouter.test.ts @@ -15,6 +15,7 @@ import { createTokenCookiesSetOptions, exchangeAuthNTokens, getCookieValuesFromNextApiRequest, + getRedirectOrDefault, resolveCodeAndStateFromUrl, resolveRedirectSignInUrl, } from '../../../src/auth/utils'; @@ -49,6 +50,7 @@ const mockGetCookieValuesFromNextApiRequest = jest.mocked( ); const mockResolveCodeAndStateFromUrl = jest.mocked(resolveCodeAndStateFromUrl); const mockResolveRedirectSignInUrl = jest.mocked(resolveRedirectSignInUrl); +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleSignInCallbackRequest', () => { const mockHandlerInput: CreateAuthRoutesHandlersInput = { @@ -67,6 +69,12 @@ describe('handleSignInCallbackRequest', () => { mockResponse, } = createMockNextApiResponse(); + beforeAll(() => { + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); + }); + afterEach(() => { mockAppendSetCookieHeadersToNextApiResponse.mockClear(); mockCreateAuthFlowProofCookiesRemoveOptions.mockClear(); @@ -78,6 +86,7 @@ describe('handleSignInCallbackRequest', () => { mockGetCookieValuesFromNextApiRequest.mockClear(); mockResolveCodeAndStateFromUrl.mockClear(); mockResolveRedirectSignInUrl.mockClear(); + mockGetRedirectOrDefault.mockClear(); mockResponseAppendHeader.mockClear(); mockResponseEnd.mockClear(); @@ -339,6 +348,9 @@ describe('handleSignInCallbackRequest', () => { ).toHaveBeenCalledWith({ redirectOnSignInComplete: expectedFinalRedirect, }); + expect(getRedirectOrDefault).toHaveBeenCalledWith( + handlerInput.redirectOnSignInComplete, + ); }, ); }); diff --git a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequest.test.ts b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequest.test.ts index 62a35effa80..5b75368f750 100644 --- a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequest.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequest.test.ts @@ -16,6 +16,7 @@ import { createTokenCookiesRemoveOptions, createTokenRemoveCookies, getCookieValuesFromRequest, + getRedirectOrDefault, revokeAuthNTokens, } from '../../../src/auth/utils'; @@ -33,6 +34,7 @@ const mockCreateTokenRemoveCookies = jest.mocked(createTokenRemoveCookies); const mockGetCookieValuesFromRequest = jest.mocked(getCookieValuesFromRequest); const mockRevokeAuthNTokens = jest.mocked(revokeAuthNTokens); const mockCreateKeysForAuthStorage = jest.mocked(createKeysForAuthStorage); +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleSignOutCallbackRequest', () => { const mockRequest = new Request( @@ -45,12 +47,19 @@ describe('handleSignOutCallbackRequest', () => { domain: '.example.com', }; + beforeAll(() => { + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); + }); + afterEach(() => { mockAppendSetCookieHeaders.mockClear(); mockCreateTokenCookiesRemoveOptions.mockClear(); mockCreateTokenRemoveCookies.mockClear(); mockGetCookieValuesFromRequest.mockClear(); mockRevokeAuthNTokens.mockClear(); + mockGetRedirectOrDefault.mockClear(); }); it(`returns a 400 response when the request does not have the "${IS_SIGNING_OUT_COOKIE_NAME}" cookie`, async () => { @@ -125,6 +134,7 @@ describe('handleSignOutCallbackRequest', () => { // verify the response expect(response.status).toBe(302); expect(response.headers.get('Location')).toBe('/'); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith(undefined); // verify the calls to dependencies expect(mockGetCookieValuesFromRequest).toHaveBeenCalledWith(mockRequest, [ @@ -284,6 +294,9 @@ describe('handleSignOutCallbackRequest', () => { mockCreateTokenRemoveCookiesResult, mockCreateTokenCookiesRemoveOptionsResult, ); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith( + handlerInput.redirectOnSignOutComplete, + ); }, ); }); diff --git a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequestForPagesRouter.test.ts b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequestForPagesRouter.test.ts index 3aedcf698a3..dbfa5bf466a 100644 --- a/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequestForPagesRouter.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/handlers/handleSignOutCallbackRequestForPagesRouter.test.ts @@ -17,6 +17,7 @@ import { createTokenCookiesRemoveOptions, createTokenRemoveCookies, getCookieValuesFromNextApiRequest, + getRedirectOrDefault, revokeAuthNTokens, } from '../../../src/auth/utils'; import { createMockNextApiResponse } from '../testUtils'; @@ -39,6 +40,7 @@ const mockGetCookieValuesFromNextApiRequest = jest.mocked( ); const mockRevokeAuthNTokens = jest.mocked(revokeAuthNTokens); const mockCreateKeysForAuthStorage = jest.mocked(createKeysForAuthStorage); +const mockGetRedirectOrDefault = jest.mocked(getRedirectOrDefault); describe('handleSignOutCallbackRequest', () => { const mockRequest = { @@ -59,12 +61,19 @@ describe('handleSignOutCallbackRequest', () => { mockResponse, } = createMockNextApiResponse(); + beforeAll(() => { + mockGetRedirectOrDefault.mockImplementation( + (redirect: string | undefined) => redirect || '/', + ); + }); + afterEach(() => { mockAppendSetCookieHeadersToNextApiResponse.mockClear(); mockCreateTokenCookiesRemoveOptions.mockClear(); mockCreateTokenRemoveCookies.mockClear(); mockGetCookieValuesFromNextApiRequest.mockClear(); mockRevokeAuthNTokens.mockClear(); + mockGetRedirectOrDefault.mockClear(); mockResponseAppendHeader.mockClear(); mockResponseEnd.mockClear(); @@ -114,6 +123,7 @@ describe('handleSignOutCallbackRequest', () => { // verify the response expect(mockResponseRedirect).toHaveBeenCalledWith(302, '/'); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith(undefined); // verify the calls to dependencies expect(mockGetCookieValuesFromNextApiRequest).toHaveBeenCalledWith( @@ -332,6 +342,9 @@ describe('handleSignOutCallbackRequest', () => { mockCreateTokenRemoveCookiesResult, mockCreateTokenCookiesRemoveOptionsResult, ); + expect(mockGetRedirectOrDefault).toHaveBeenCalledWith( + handlerInput.redirectOnSignOutComplete, + ); }, ); }); diff --git a/packages/adapter-nextjs/__tests__/auth/utils/cognitoHostedUIEndpoints.test.ts b/packages/adapter-nextjs/__tests__/auth/utils/cognitoHostedUIEndpoints.test.ts index 94a8e34838c..6d8928fd105 100644 --- a/packages/adapter-nextjs/__tests__/auth/utils/cognitoHostedUIEndpoints.test.ts +++ b/packages/adapter-nextjs/__tests__/auth/utils/cognitoHostedUIEndpoints.test.ts @@ -44,7 +44,6 @@ describe('cognitoHostedUIEndpoints', () => { test.each(testCase)( 'factory %s returns expected url: %s', (fn, expected, args) => { - // eslint-disable-next-line import/namespace expect(cognitoHostedUIEndpoints[fn].apply(null, args)).toBe(expected); }, ); diff --git a/packages/adapter-nextjs/__tests__/auth/utils/getRedirectOrDefault.test.ts b/packages/adapter-nextjs/__tests__/auth/utils/getRedirectOrDefault.test.ts new file mode 100644 index 00000000000..3c6f6cdc60d --- /dev/null +++ b/packages/adapter-nextjs/__tests__/auth/utils/getRedirectOrDefault.test.ts @@ -0,0 +1,11 @@ +import { getRedirectOrDefault } from '../../../src/auth/utils/getRedirectOrDefault'; + +describe('getRedirectOrDefault', () => { + test.each([ + [undefined, '/'], + ['', '/'], + ['/home', '/home'], + ])('when input redirect is `%s` returns `%s`', (input, output) => { + expect(getRedirectOrDefault(input)).toBe(output); + }); +}); diff --git a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts index ebcf44e7b1a..8ff7061b0e5 100644 --- a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts +++ b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForAppRouter.ts @@ -3,6 +3,7 @@ import { HandleAuthApiRouteRequestForAppRouter } from './types'; import { + getRedirectOrDefault, hasActiveUserSessionWithAppRouter, isSupportedAuthApiRoutePath, } from './utils'; @@ -49,7 +50,9 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor return new Response(null, { status: 302, headers: new Headers({ - Location: handlerInput.redirectOnSignInComplete ?? '/', + Location: getRedirectOrDefault( + handlerInput.redirectOnSignInComplete, + ), }), }); } @@ -74,7 +77,9 @@ export const handleAuthApiRouteRequestForAppRouter: HandleAuthApiRouteRequestFor return new Response(null, { status: 302, headers: new Headers({ - Location: handlerInput.redirectOnSignInComplete ?? '/', + Location: getRedirectOrDefault( + handlerInput.redirectOnSignInComplete, + ), }), }); } diff --git a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts index 2a80a8b5ab0..014f68721bc 100644 --- a/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts +++ b/packages/adapter-nextjs/src/auth/handleAuthApiRouteRequestForPagesRouter.ts @@ -3,6 +3,7 @@ import { HandleAuthApiRouteRequestForPagesRouter } from './types'; import { + getRedirectOrDefault, hasActiveUserSessionWithPagesRouter, isSupportedAuthApiRoutePath, } from './utils'; @@ -53,7 +54,10 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF }); if (hasActiveUserSession) { - response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/'); + response.redirect( + 302, + getRedirectOrDefault(handlerInput.redirectOnSignInComplete), + ); return; } @@ -78,7 +82,10 @@ export const handleAuthApiRouteRequestForPagesRouter: HandleAuthApiRouteRequestF }); if (hasActiveUserSession) { - response.redirect(302, handlerInput.redirectOnSignInComplete ?? '/'); + response.redirect( + 302, + getRedirectOrDefault(handlerInput.redirectOnSignInComplete), + ); return; } diff --git a/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequest.ts b/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequest.ts index e1689c8f73d..69533724ed6 100644 --- a/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequest.ts +++ b/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequest.ts @@ -11,6 +11,7 @@ import { createTokenCookiesSetOptions, exchangeAuthNTokens, getCookieValuesFromRequest, + getRedirectOrDefault, resolveCodeAndStateFromUrl, resolveRedirectSignInUrl, } from '../utils'; @@ -74,7 +75,9 @@ export const handleSignInCallbackRequest: HandleSignInCallbackRequest = async ({ return new Response( createOnSignInCompleteRedirectIntermediate({ - redirectOnSignInComplete: handlerInput.redirectOnSignInComplete || '/', + redirectOnSignInComplete: getRedirectOrDefault( + handlerInput.redirectOnSignInComplete, + ), }), { status: 200, diff --git a/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequestForPagesRouter.ts b/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequestForPagesRouter.ts index e7210530759..843f931be21 100644 --- a/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequestForPagesRouter.ts +++ b/packages/adapter-nextjs/src/auth/handlers/handleSignInCallbackRequestForPagesRouter.ts @@ -11,6 +11,7 @@ import { createTokenCookiesSetOptions, exchangeAuthNTokens, getCookieValuesFromNextApiRequest, + getRedirectOrDefault, resolveCodeAndStateFromUrl, resolveRedirectSignInUrl, } from '../utils'; @@ -86,8 +87,9 @@ export const handleSignInCallbackRequestForPagesRouter: HandleSignInCallbackRequ .status(200) .send( createOnSignInCompleteRedirectIntermediate({ - redirectOnSignInComplete: - handlerInput.redirectOnSignInComplete || '/', + redirectOnSignInComplete: getRedirectOrDefault( + handlerInput.redirectOnSignInComplete, + ), }), ); }; diff --git a/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequest.ts b/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequest.ts index d7c6d761d02..2d4b06085ea 100644 --- a/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequest.ts +++ b/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequest.ts @@ -12,6 +12,7 @@ import { createTokenCookiesRemoveOptions, createTokenRemoveCookies, getCookieValuesFromRequest, + getRedirectOrDefault, revokeAuthNTokens, } from '../utils'; @@ -40,7 +41,9 @@ export const handleSignOutCallbackRequest: HandleSignOutCallbackRequest = return new Response(null, { status: 302, headers: new Headers({ - Location: handlerInput.redirectOnSignOutComplete ?? '/', + Location: getRedirectOrDefault( + handlerInput.redirectOnSignOutComplete, + ), }), }); } @@ -56,7 +59,9 @@ export const handleSignOutCallbackRequest: HandleSignOutCallbackRequest = return new Response(null, { status: 302, headers: new Headers({ - Location: handlerInput.redirectOnSignOutComplete ?? '/', + Location: getRedirectOrDefault( + handlerInput.redirectOnSignOutComplete, + ), }), }); } @@ -84,7 +89,10 @@ export const handleSignOutCallbackRequest: HandleSignOutCallbackRequest = createTokenCookiesRemoveOptions(setCookieOptions), ); - headers.set('Location', handlerInput.redirectOnSignOutComplete ?? '/'); + headers.set( + 'Location', + getRedirectOrDefault(handlerInput.redirectOnSignOutComplete), + ); return new Response(null, { status: 302, diff --git a/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequestForPagesRouter.ts b/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequestForPagesRouter.ts index 42afa805591..9dc7de354e8 100644 --- a/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequestForPagesRouter.ts +++ b/packages/adapter-nextjs/src/auth/handlers/handleSignOutCallbackRequestForPagesRouter.ts @@ -12,6 +12,7 @@ import { createTokenCookiesRemoveOptions, createTokenRemoveCookies, getCookieValuesFromNextApiRequest, + getRedirectOrDefault, revokeAuthNTokens, } from '../utils'; @@ -40,7 +41,10 @@ export const handleSignOutCallbackRequestForPagesRouter: HandleSignOutCallbackRe getCookieValuesFromNextApiRequest(request, [lastAuthUserCookieName]); if (!username) { - response.redirect(302, handlerInput.redirectOnSignOutComplete ?? '/'); + response.redirect( + 302, + getRedirectOrDefault(handlerInput.redirectOnSignOutComplete), + ); return; } @@ -56,7 +60,10 @@ export const handleSignOutCallbackRequestForPagesRouter: HandleSignOutCallbackRe ]); if (!refreshToken) { - response.redirect(302, handlerInput.redirectOnSignOutComplete ?? '/'); + response.redirect( + 302, + getRedirectOrDefault(handlerInput.redirectOnSignOutComplete), + ); return; } @@ -85,5 +92,8 @@ export const handleSignOutCallbackRequestForPagesRouter: HandleSignOutCallbackRe createTokenCookiesRemoveOptions(setCookieOptions), ); - response.redirect(302, handlerInput.redirectOnSignOutComplete ?? '/'); + response.redirect( + 302, + getRedirectOrDefault(handlerInput.redirectOnSignOutComplete), + ); }; diff --git a/packages/adapter-nextjs/src/auth/utils/getRedirectOrDefault.ts b/packages/adapter-nextjs/src/auth/utils/getRedirectOrDefault.ts new file mode 100644 index 00000000000..0be30a78601 --- /dev/null +++ b/packages/adapter-nextjs/src/auth/utils/getRedirectOrDefault.ts @@ -0,0 +1,5 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +export const getRedirectOrDefault = (redirect: string | undefined): string => + redirect || '/'; diff --git a/packages/adapter-nextjs/src/auth/utils/index.ts b/packages/adapter-nextjs/src/auth/utils/index.ts index 9d861e1c841..fda5e5bcaff 100644 --- a/packages/adapter-nextjs/src/auth/utils/index.ts +++ b/packages/adapter-nextjs/src/auth/utils/index.ts @@ -23,6 +23,7 @@ export { export { getAccessTokenUsernameAndClockDrift } from './getAccessTokenUsernameAndClockDrift'; export { getCookieValuesFromNextApiRequest } from './getCookieValuesFromNextApiRequest'; export { getCookieValuesFromRequest } from './getCookieValuesFromRequest'; +export { getRedirectOrDefault } from './getRedirectOrDefault'; export { isAuthRoutesHandlersContext, isNextApiRequest,