Skip to content

Commit

Permalink
docs: add missing authorized callback (#11287)
Browse files Browse the repository at this point in the history
* fix(docs): missing authorized callback

* simplify the example

---------

Co-authored-by: davide.tanca <[email protected]>
Co-authored-by: Thang Vu <[email protected]>
  • Loading branch information
3 people authored Jun 29, 2024
1 parent aefae63 commit fd809cf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/pages/getting-started/session-management/protecting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ With Next.js 12+, the easiest way to protect a set of pages is using the middlew
export { auth as middleware } from "@/auth"
```

Then define `authorized` callback in your `auth.ts` file. For more details check out the [reference docs](/reference/nextjs#authorized).

```ts filename="auth.ts"
import NextAuth from "next-auth"

export const { auth, handlers } = NextAuth({
callbacks: {
authorized: async ({ auth }) => {
// Logged in users are authenticated, otherwise redirect to login page
return !!auth
},
},
})
```

You can also use the `auth` method as a wrapper if you'd like to implement more logic inside the middleware.

```ts filename="middleware.ts"
Expand Down

0 comments on commit fd809cf

Please sign in to comment.