Skip to content

Commit

Permalink
fix(middleware): allow secret as option in Middleware (#4846)
Browse files Browse the repository at this point in the history
* ✨ 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>
  • Loading branch information
sean-nicholas and balazsorban44 committed Jul 7, 2022
1 parent 3c210d9 commit c59a4e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
21 changes: 19 additions & 2 deletions docs/docs/configuration/nextjs.md
Expand Up @@ -80,10 +80,11 @@ You can get the `withAuth` middleware function from `next-auth/middleware` eithe

### Prerequisites

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

**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.
Alternatively, you can provide the secret using the [`secret`](#secret) option in the middleware config.

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

### Basic usage

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

---

### `secret`

- **Required**: _No_

#### Description

The same `secret` used in the [NextAuth config](/configuration/options#options).

#### Example (default value)

```js
secret: process.env.NEXTAUTH_SECRET
```

---

### Advanced usage

NextAuth.js Middleware is very flexible, there are multiple ways to use it.
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/configuration/options.md
Expand Up @@ -27,9 +27,8 @@ Using [System Environment Variables](https://vercel.com/docs/concepts/projects/e

### NEXTAUTH_SECRET

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.
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).

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

### NEXTAUTH_URL_INTERNAL

Expand Down
10 changes: 9 additions & 1 deletion packages/next-auth/src/next/middleware.ts
Expand Up @@ -84,6 +84,12 @@ export interface NextAuthMiddlewareOptions {
*/
authorized?: AuthorizedCallback
}

/**
* The same `secret` used in the `NextAuth` configuration.
* Defaults to the `NEXTAUTH_SECRET` environment variable.
*/
secret?: string
}

async function handleMiddleware(
Expand All @@ -102,7 +108,8 @@ async function handleMiddleware(
return
}

if (!process.env.NEXTAUTH_SECRET) {
const secret = options?.secret ?? process.env.NEXTAUTH_SECRET
if (!secret) {
console.error(
`[next-auth][error][NO_SECRET]`,
`\nhttps://next-auth.js.org/errors#no_secret`
Expand All @@ -118,6 +125,7 @@ async function handleMiddleware(
req,
decode: options?.jwt?.decode,
cookieName: options?.cookies?.sessionToken?.name,
secret,
})

const isAuthorized =
Expand Down

1 comment on commit c59a4e0

@vercel
Copy link

@vercel vercel bot commented on c59a4e0 Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.