From 834215f24197f20e126393b114176f7b990bb2fd Mon Sep 17 00:00:00 2001 From: Hui Zhao Date: Sat, 21 Sep 2024 10:16:11 -0700 Subject: [PATCH] fix(adapter-nextjs): make createAuthRouteHandlers interface work in both App and Pages routers --- .../src/auth/createAuthRouteHandlersFactory.ts | 4 ++-- packages/adapter-nextjs/src/auth/types.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/adapter-nextjs/src/auth/createAuthRouteHandlersFactory.ts b/packages/adapter-nextjs/src/auth/createAuthRouteHandlersFactory.ts index 582450280b3..85d6499f47d 100644 --- a/packages/adapter-nextjs/src/auth/createAuthRouteHandlersFactory.ts +++ b/packages/adapter-nextjs/src/auth/createAuthRouteHandlersFactory.ts @@ -11,9 +11,9 @@ import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-c import { AuthRoutesHandlerContext, - CreateAuthRouteHandlers, CreateAuthRouteHandlersFactoryInput, CreateAuthRoutesHandlersInput, + InternalCreateAuthRouteHandlers, } from './types'; import { isAuthRoutesHandlersContext, @@ -29,7 +29,7 @@ export const createAuthRouteHandlersFactory = ({ runtimeOptions = {}, amplifyAppOrigin, runWithAmplifyServerContext, -}: CreateAuthRouteHandlersFactoryInput): CreateAuthRouteHandlers => { +}: CreateAuthRouteHandlersFactoryInput): InternalCreateAuthRouteHandlers => { if (!amplifyAppOrigin) throw new AmplifyServerContextError({ message: 'Could not find the AMPLIFY_APP_ORIGIN environment variable.', diff --git a/packages/adapter-nextjs/src/auth/types.ts b/packages/adapter-nextjs/src/auth/types.ts index 400c70edc31..72376663909 100644 --- a/packages/adapter-nextjs/src/auth/types.ts +++ b/packages/adapter-nextjs/src/auth/types.ts @@ -66,10 +66,20 @@ export interface CreateAuthRoutesHandlersInput { redirectOnSignOutComplete?: string; } -export type CreateAuthRouteHandlers = ( +export type InternalCreateAuthRouteHandlers = ( input?: CreateAuthRoutesHandlersInput, ) => AuthRouteHandlers; +export type CreateAuthRouteHandlers = ( + input?: CreateAuthRoutesHandlersInput, +) => + | AuthRouteHandlers + // Forcing the handler interface to be any to ensure the single handler can + // work in both App Router `routes.ts` and Pages router. The former has a + // restrict handler function interface type check. The parameters types are + // properly typed internally, and runtime validation is place. + | any; + export interface CreateAuthRouteHandlersFactoryInput { config: ResourcesConfig; runtimeOptions: NextServer.CreateServerRunnerRuntimeOptions | undefined;