Skip to content

Commit 9b15388

Browse files
committed
fix(core): detect Vercel without NEXTAUTH_URL (nextauthjs#3649)
* fix(core): detect Vercel without `NEXTAUTH_URL` * chore(ts): use `any` * chore: use `process.env.VERCEL` to detect Vercel
1 parent 3d45fe6 commit 9b15388

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/jwt/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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",

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type NextAuthAction =
5050
export 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

src/next/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextAuthHandler } from "../core"
2-
import { setCookie } from "./cookie"
2+
import { setCookie, detectHost } from "./utils"
33

44
import 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
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)