From 0779333c451811b0517ff624d3637d276fbee025 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 6e3b4389d7b..4dea18ae700 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, @@ -28,7 +28,7 @@ export const createAuthRouteHandlersFactory = ({ config: resourcesConfig, runtimeOptions = {}, runWithAmplifyServerContext, -}: CreateAuthRouteHandlersFactoryInput): CreateAuthRouteHandlers => { +}: CreateAuthRouteHandlersFactoryInput): InternalCreateAuthRouteHandlers => { const origin = process.env.AMPLIFY_APP_ORIGIN; if (!origin) throw new AmplifyServerContextError({ diff --git a/packages/adapter-nextjs/src/auth/types.ts b/packages/adapter-nextjs/src/auth/types.ts index 199e4eeb8f7..d8075fea708 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;