Skip to content

Commit c59a4e0

Browse files
sean-nicholasbalazsorban44
andauthoredJul 7, 2022
fix(middleware): allow secret as option in Middleware (#4846)
* ✨ provide secret via config * 🐛 make secret optional * 📝 docs for middleware and env var * 📝 recommendation at the end of paragraph Co-authored-by: Balázs Orbán <info@balazsorban.com>
1 parent 3c210d9 commit c59a4e0

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed
 

‎docs/docs/configuration/nextjs.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ You can get the `withAuth` middleware function from `next-auth/middleware` eithe
8080

8181
### Prerequisites
8282

83-
You must set the [`NEXTAUTH_SECRET`](/configuration/options#nextauth_secret) environment variable when using this middleware. If you are using the [`secret` option](/configuration/options#secret) this value must match.
83+
You must set the same secret in the middleware that you use in NextAuth. The easiest way is to set the [`NEXTAUTH_SECRET`](/configuration/options#nextauth_secret) environment variable. It will be picked up by both the [NextAuth config](/configuration/options#options), as well as the middleware config.
8484

85-
**We strongly recommend** replacing the `secret` value completely with this `NEXTAUTH_SECRET` environment variable. This environment variable will be picked up by both the [NextAuth config](/configuration/options#options), as well as the middleware config.
85+
Alternatively, you can provide the secret using the [`secret`](#secret) option in the middleware config.
8686

87+
**We strongly recommend** replacing the `secret` value completely with this `NEXTAUTH_SECRET` environment variable.
8788

8889
### Basic usage
8990

@@ -149,6 +150,22 @@ See the documentation for the [pages option](/configuration/pages) for more info
149150

150151
---
151152

153+
### `secret`
154+
155+
- **Required**: _No_
156+
157+
#### Description
158+
159+
The same `secret` used in the [NextAuth config](/configuration/options#options).
160+
161+
#### Example (default value)
162+
163+
```js
164+
secret: process.env.NEXTAUTH_SECRET
165+
```
166+
167+
---
168+
152169
### Advanced usage
153170

154171
NextAuth.js Middleware is very flexible, there are multiple ways to use it.

‎docs/docs/configuration/options.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ Using [System Environment Variables](https://vercel.com/docs/concepts/projects/e
2727

2828
### NEXTAUTH_SECRET
2929

30-
Used to encrypt the NextAuth.js JWT, and to hash [email verification tokens](/adapters/models#verification-token). This is the default value for the [`secret`](/configuration/options#secret) option. The `secret` option might be removed in the future in favor of this.
30+
Used to encrypt the NextAuth.js JWT, and to hash [email verification tokens](/adapters/models#verification-token). This is the default value for the `secret` option in [NextAuth](/configuration/options#secret) and [Middleware](/configuration/nextjs#secret).
3131

32-
If you are using [Middleware](/configuration/nextjs#prerequisites) this environment variable must be set.
3332

3433
### NEXTAUTH_URL_INTERNAL
3534

‎packages/next-auth/src/next/middleware.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export interface NextAuthMiddlewareOptions {
8484
*/
8585
authorized?: AuthorizedCallback
8686
}
87+
88+
/**
89+
* The same `secret` used in the `NextAuth` configuration.
90+
* Defaults to the `NEXTAUTH_SECRET` environment variable.
91+
*/
92+
secret?: string
8793
}
8894

8995
async function handleMiddleware(
@@ -102,7 +108,8 @@ async function handleMiddleware(
102108
return
103109
}
104110

105-
if (!process.env.NEXTAUTH_SECRET) {
111+
const secret = options?.secret ?? process.env.NEXTAUTH_SECRET
112+
if (!secret) {
106113
console.error(
107114
`[next-auth][error][NO_SECRET]`,
108115
`\nhttps://next-auth.js.org/errors#no_secret`
@@ -118,6 +125,7 @@ async function handleMiddleware(
118125
req,
119126
decode: options?.jwt?.decode,
120127
cookieName: options?.cookies?.sessionToken?.name,
128+
secret,
121129
})
122130

123131
const isAuthorized =

1 commit comments

Comments
 (1)
Please sign in to comment.