File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ export async function getToken<R extends boolean = false>(
7171 const {
7272 req,
7373 secureCookie = process . env . NEXTAUTH_URL ?. startsWith ( "https://" ) ??
74- ! ! process . env . VERCEL_URL ,
74+ ! ! process . env . VERCEL ,
7575 cookieName = secureCookie
7676 ? "__Secure-next-auth.session-token"
7777 : "next-auth.session-token" ,
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ export type NextAuthAction =
5050export interface InternalOptions < T extends ProviderType = any > {
5151 providers : InternalProvider [ ]
5252 /**
53- * Parsed from `NEXTAUTH_URL` or `VERCEL_URL` .
53+ * Parsed from `NEXTAUTH_URL` or `x-forwarded-host` on Vercel .
5454 * @default "http://localhost:3000/api/auth"
5555 */
5656 url : InternalUrl
Original file line number Diff line number Diff line change 11import { NextAuthHandler } from "../core"
2- import { setCookie } from "./cookie "
2+ import { setCookie , detectHost } from "./utils "
33
44import type {
55 GetServerSidePropsContext ,
@@ -21,7 +21,7 @@ async function NextAuthNextHandler(
2121 const { nextauth, ...query } = req . query
2222 const handler = await NextAuthHandler ( {
2323 req : {
24- host : process . env . NEXTAUTH_URL ?? process . env . VERCEL_URL ,
24+ host : detectHost ( req . headers [ "x-forwarded-host" ] ) ,
2525 body : req . body ,
2626 query,
2727 cookies : req . cookies ,
@@ -87,7 +87,7 @@ export async function getServerSession(
8787 const session = await NextAuthHandler < Session | { } > ( {
8888 options,
8989 req : {
90- host : process . env . NEXTAUTH_URL ?? process . env . VERCEL_URL ,
90+ host : detectHost ( context . req . headers [ "x-forwarded-host" ] ) ,
9191 action : "session" ,
9292 method : "GET" ,
9393 cookies : context . req . cookies ,
@@ -108,7 +108,7 @@ declare global {
108108 namespace NodeJS {
109109 interface ProcessEnv {
110110 NEXTAUTH_URL ?: string
111- VERCEL_URL ?: string
111+ VERCEL ?: "1"
112112 }
113113 }
114114}
Original file line number Diff line number Diff line change @@ -13,3 +13,11 @@ export function setCookie(res, cookie: Cookie) {
1313 setCookieHeader . push ( cookieHeader )
1414 res . setHeader ( "Set-Cookie" , setCookieHeader )
1515}
16+
17+ /** Extract the host from the environment */
18+ export function detectHost ( forwardedHost : any ) {
19+ // If we detect a Vercel environment, we can trust the host
20+ if ( process . env . VERCEL ) return forwardedHost
21+ // If `NEXTAUTH_URL` is `undefined` we fall back to "http://localhost:3000"
22+ return process . env . NEXTAUTH_URL
23+ }
You can’t perform that action at this time.
0 commit comments