In my API endpoint, it needed the user's session to verify if the user is authorized to make this request. In order to do so, I use
const session = await getToken({ req, secret: process.env.NEXTAUTH_SECRET })
This is the formal and correct way. However, it also works perfectly fine if I do this without the secret:
const session = await getToken({ req })
If I don't explicitly pass the secret into the getToken method, does it automatically get it from the NextAuth
object from [...nextauth].js
?
You're only required to pass the secret to the getToken
call if you have not set the NEXTAUTH_SECRET
environment variable.
The
getToken()
helper requires the following options:
req
- (object) Request objectsecret
- (string) JWT Secret. UseNEXTAUTH_SECRET
instead.
In your case, since you have set NEXTAUTH_SECRET
you do not need to pass secret
to getToken
.