fix(auth): widen AuthBundle.handlers to (Request) => Promise<Response>
NextAuthResult["handlers"] embeds NextRequest from the kit's own next/node_modules copy, which conflicts with the consumer's next and produces a spurious RouteHandlerConfig mismatch in .next/types/validator.ts for every app's [...nextauth]/route.ts. Replace the inferred type with a structural AuthRouteHandlers shape using web-standard Request/Response. NextRequest extends Request, so function-parameter contravariance makes this assignable wherever Next's validator wants (request: NextRequest, ctx) => ... Cast through unknown at the return site since TS can't prove the contravariance across the two next copies on its own. Verified: gscCRM `npm run build` clean, `tsc --noEmit` no longer flags validator.ts. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,13 +6,31 @@ import type { CreateAuthOptions, SessionUser } from "./types";
|
|||||||
|
|
||||||
export type { SessionUser, CreateAuthOptions };
|
export type { SessionUser, CreateAuthOptions };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shape of the GET/POST route handlers NextAuth returns. Declared
|
||||||
|
* structurally (Request → Response) rather than via
|
||||||
|
* `NextAuthResult["handlers"]` — that type embeds `NextRequest` from
|
||||||
|
* the kit's own copy of `next`, which conflicts with the consumer's
|
||||||
|
* `next` and produces a spurious `RouteHandlerConfig` mismatch in
|
||||||
|
* `.next/types/validator.ts` for every app's `[...nextauth]/route.ts`.
|
||||||
|
*
|
||||||
|
* Web-standard `Request`/`Response` are valid for Next.js route
|
||||||
|
* handlers (NextRequest extends Request), and TS function-parameter
|
||||||
|
* contravariance makes this assignable wherever the validator wants
|
||||||
|
* `(request: NextRequest, ctx) => ...`.
|
||||||
|
*/
|
||||||
|
export type AuthRouteHandlers = {
|
||||||
|
GET: (request: Request) => Promise<Response>;
|
||||||
|
POST: (request: Request) => Promise<Response>;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Result of `createAuth()`. Mirrors NextAuth's return value plus
|
* Result of `createAuth()`. Mirrors NextAuth's return value plus
|
||||||
* `requireAuth` and the resolved `signInPath` so consumers don't have to
|
* `requireAuth` and the resolved `signInPath` so consumers don't have to
|
||||||
* thread these around.
|
* thread these around.
|
||||||
*/
|
*/
|
||||||
export interface AuthBundle {
|
export interface AuthBundle {
|
||||||
handlers: NextAuthResult["handlers"];
|
handlers: AuthRouteHandlers;
|
||||||
signIn: NextAuthResult["signIn"];
|
signIn: NextAuthResult["signIn"];
|
||||||
signOut: NextAuthResult["signOut"];
|
signOut: NextAuthResult["signOut"];
|
||||||
auth: NextAuthResult["auth"];
|
auth: NextAuthResult["auth"];
|
||||||
@@ -114,7 +132,12 @@ export function createAuth(opts: CreateAuthOptions): AuthBundle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
handlers: na.handlers,
|
// Cast through unknown: NextAuth's handler param type references its
|
||||||
|
// own NextRequest; the AuthRouteHandlers shape is structurally
|
||||||
|
// compatible (NextRequest extends Request), but TS can't prove the
|
||||||
|
// function-parameter contravariance across the two `next` copies on
|
||||||
|
// its own.
|
||||||
|
handlers: na.handlers as unknown as AuthRouteHandlers,
|
||||||
signIn: na.signIn,
|
signIn: na.signIn,
|
||||||
signOut: na.signOut,
|
signOut: na.signOut,
|
||||||
auth: na.auth,
|
auth: na.auth,
|
||||||
|
|||||||
Reference in New Issue
Block a user