Closed BleedingDev closed 4 weeks ago
Hi @BleedingDev!
This is kind of expected based on your code and not a NextAuth.js bug, just how JavaScript works, but I understand that it might be confusing at first sight, so let me try to explain.
Since NextAuth(authOptions)
is outside the request handlers, our secret assertion logic inside NextAuth
will run before the secret has been loaded into your runtime.
I actually don't think it's related to Vercel, try next build && next start
locally, and you'll have the same issue.
You can solve this by using eg. advanced initialization: https://next-auth.js.org/configuration/initialization#advanced-initialization
I think this file needs to be changed like so:
import NextAuth from 'next-auth'
import { authOptions } from './auth-options'
- const handler = NextAuth(authOptions)
- export { handler as GET, handler as POST }
+ export const GET = (...args) => NextAuth(authOptions)(...args)
+ export const POST = (...args) => NextAuth(authOptions)(...args)
This will ensure that NextAuth
is only invoked after your secrets have been initialized, passing the secret assertion logic!
Let me know if this resolves your issue!
I will close, feel free to open a new report if you have further questions!
Thanks for your reply, I'll try to change it. 😊
Environment
System: OS: Windows 11 10.0.26120 CPU: (16) x64 AMD Ryzen 7 6800HS with Radeon Graphics Memory: 10.40 GB / 31.26 GB Binaries: Node: 20.17.0 - ~.proto\shims\node.EXE npm: 10.8.1 - ~.proto\shims\npm.EXE bun: 1.1.29 - ~.proto\shims\bun.EXE Browsers: Edge: Chromium (130.0.2849.13) Internet Explorer: 11.0.26100.1 npmPackages: @auth/prisma-adapter: ^2.6.0 => 2.7.0 next: 15.0.0-canary.170 => 15.0.0-canary.170 next-auth: ^4.24.8 => 4.24.8 react: 19.0.0-rc-1460d67c-20241003 => 19.0.0-rc-1460d67c-20241003
Reproduction URL
https://github.com/NaucMeIT/web/pull/465
Describe the issue
I decided to eliminate
process.env
from my codebase and have type-safe and secure secrets using Infisical + EffectTS, everything went smooth only except Vercel runtime check.Here's my
authOptions
config:As you can see I am passing secret, but when
NEXTAUTH_SECRET
is not defined in secrets in Vercel's environment variables, it just doesn't work. I checked multiple times that during build-time the variable is defined and is correctly passed into the object.But it still fails with this error:
How to reproduce
Demo of error: https://coursition-git-add-typesafe-config-naucmeits-projects.vercel.app/
Expected behavior
Normally working when manually passing
secret
in config.