AUTH_SECRET vs NEXTAUTH_SECRET
| Auth.js v5 (next-auth@5) | NextAuth.js v4 | |
|---|---|---|
| Secret variable | AUTH_SECRET | NEXTAUTH_SECRET |
| URL variable | AUTH_URL (usually inferred) | NEXTAUTH_URL |
| Generate | npx auth secret | openssl rand -base64 32 |
| Error when missing | MissingSecret | NO_SECRET |
| Rotation | secret: [new, old] | not built in |
Both versions accept the same kind of value: any long random string. The generator gives you both lines so you can copy the one your package.json needs.
Adding the secret to Next.js
AUTH_SECRET=<paste 44-character value> AUTH_GITHUB_ID=Iv1.xxxxxxxx AUTH_GITHUB_SECRET=xxxxxxxx
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
// AUTH_SECRET and AUTH_GITHUB_* are read from the environment automatically
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [GitHub],
});Then add AUTH_SECRET with a different value in your host's settings for production. On Vercel that is Project → Settings → Environment Variables; the guide Vercel environment variables explains which scope to pick. For the full Next.js .env.local, use the Next.js .env generator.
Rotating the secret without logging everyone out
export const { handlers, auth } = NextAuth({
// first entry encrypts new sessions, all entries can decrypt
secret: [process.env.AUTH_SECRET!, process.env.AUTH_SECRET_OLD!].filter(Boolean),
providers: [/* … */],
});Deploy the new value as AUTH_SECRET and the previous one as AUTH_SECRET_OLD. After the session lifetime (30 days by default) remove the old one. If the old secret leaked, skip this step: every session encrypted with it should end now.