.env · security🔥 popular🔒 100% client-side — never leaves your browser

NextAuth Secret Generator (AUTH_SECRET)

Generate a NextAuth secret in one click: this NextAuth secret generator creates AUTH_SECRET for Auth.js v5 and NEXTAUTH_SECRET for NextAuth.js v4 from 32 random bytes, the same format as npx auth secret and openssl rand -base64 32.

By Aliniyaz Mamat · last reviewed

Be the first to rate
generated locally · crypto.getRandomValues · nothing sent anywhere
Auth.js v5 (Next.js 14+)
AUTH_SECRET — recommended for Auth.js v5 / next-auth v5
Raw secret
.env line
AUTH_SECRET=
NextAuth.js v4
NEXTAUTH_SECRET — legacy variable for next-auth@4
Raw secret
.env line
NEXTAUTH_SECRET=
JWT Secret (HS256/HS384, 48 bytes)
Higher entropy for custom JWT signing
Raw secret
.env line
JWT_SECRET=
CLI alternative
Auth.js built-in
npx auth secret
OpenSSL
openssl rand -base64 32

Key takeaways

  • ✓32 random bytes, base64-encoded = 44 characters with = padding, 256 bits of entropy.
  • ✓Auth.js v5 reads AUTH_SECRET; NextAuth.js v4 reads NEXTAUTH_SECRET. Use the one that matches your installed version.
  • ✓Without a secret, production fails with MissingSecret (v5) or NO_SECRET (v4). .env.local is never deployed, so set it on your host too.
  • ✓The secret encrypts JWT sessions. Anyone who has it can forge a session cookie for any user.
  • ✓Rotation without logging users out: pass secret: [newSecret, oldSecret], Auth.js decrypts with both and encrypts with the first.

What it does

  • ▸AUTH_SECRET for Auth.js v5 / next-auth@5
  • ▸NEXTAUTH_SECRET for NextAuth.js v4
  • ▸Extra 48-byte base64url secret for custom JWT signing
  • ▸Same format as npx auth secret and openssl rand -base64 32
  • ▸Generated locally with crypto.getRandomValues

Privacy

Runs 100% in your browser. Your .env never touches our servers.

client-side only

When to use this tool

  • →Setting up Auth.js in a new Next.js app
  • →Adding the secret to Vercel, Netlify or a Docker host before the first production deploy
  • →Fixing [auth][error] MissingSecret or [next-auth][error][NO_SECRET]
  • →Rotating the secret after it was committed or shared

Common mistakes

  • ⚠Setting the secret only in .env.local: it never reaches production, so the deployed app crashes on the first sign-in
  • ⚠Using v4's NEXTAUTH_SECRET name in a fresh v5 setup. v5 prefers AUTH_SECRET; keep one name across all environments
  • ⚠Reusing the development secret in production: anyone with your local .env.local can forge production sessions
  • ⚠Prefixing it with NEXT_PUBLIC_, which ships it to every browser

AUTH_SECRET vs NEXTAUTH_SECRET

Auth.js v5 (next-auth@5)NextAuth.js v4
Secret variableAUTH_SECRETNEXTAUTH_SECRET
URL variableAUTH_URL (usually inferred)NEXTAUTH_URL
Generatenpx auth secretopenssl rand -base64 32
Error when missingMissingSecretNO_SECRET
Rotationsecret: [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

.env.local
AUTH_SECRET=<paste 44-character value>
AUTH_GITHUB_ID=Iv1.xxxxxxxx
AUTH_GITHUB_SECRET=xxxxxxxx
auth.ts
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

auth.ts
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.

Frequently asked questions

AUTH_SECRET or NEXTAUTH_SECRET: which one do I use?

AUTH_SECRET for Auth.js v5 (next-auth@5), NEXTAUTH_SECRET for NextAuth.js v4. Check the version in package.json. The value format is the same for both.

Is this the same as openssl rand -base64 32?

Yes. Both produce 32 random bytes encoded as standard base64, 44 characters ending in =. npx auth secret generates the same kind of value and writes it to .env.local.

What happens if AUTH_SECRET leaks?

With JWT sessions (the default), an attacker can create a valid session for any user. Generate a new secret, deploy it without the rotation fallback, and treat existing sessions as compromised.

Do I need AUTH_URL or NEXTAUTH_URL too?

On Vercel and most platforms Auth.js v5 detects the URL itself (AUTH_TRUST_HOST). Behind your own reverse proxy, set AUTH_URL (v5) or NEXTAUTH_URL (v4) to the public URL.

Is the secret generated on your server?

No. It is created with crypto.getRandomValues in your browser and never transmitted.

Related tools

Learn more

Compare

coming soon

Get notified when env syncing launches

We're building a tiny tool to keep .env files in sync across teammates and environments. Leave your email — no spam, just a single launch ping.

We use your address only for this launch email. You can ask us to delete it at any time. Privacy policy