Skip to content

Commit fd809cf

Browse files
gdavidetancadavide.tancaThangHuuVu
authored
docs: add missing authorized callback (#11287)
* fix(docs): missing authorized callback * simplify the example --------- Co-authored-by: davide.tanca <[email protected]> Co-authored-by: Thang Vu <[email protected]>
1 parent aefae63 commit fd809cf

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/pages/getting-started/session-management/protecting.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ With Next.js 12+, the easiest way to protect a set of pages is using the middlew
218218
export { auth as middleware } from "@/auth"
219219
```
220220

221+
Then define `authorized` callback in your `auth.ts` file. For more details check out the [reference docs](/reference/nextjs#authorized).
222+
223+
```ts filename="auth.ts"
224+
import NextAuth from "next-auth"
225+
226+
export const { auth, handlers } = NextAuth({
227+
callbacks: {
228+
authorized: async ({ auth }) => {
229+
// Logged in users are authenticated, otherwise redirect to login page
230+
return !!auth
231+
},
232+
},
233+
})
234+
```
235+
221236
You can also use the `auth` method as a wrapper if you'd like to implement more logic inside the middleware.
222237

223238
```ts filename="middleware.ts"

0 commit comments

Comments
 (0)