Skip to content

Commit 4c54dfd

Browse files
committed
docs: Improve setup section
1 parent baa762d commit 4c54dfd

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

packages/payload-authjs/README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ Install the plugin using any JavaScript package manager such as PNPM, NPM, or Ya
2727
pnpm i payload-authjs
2828
```
2929

30-
First of all, this plugin only integrates Auth.js into Payload CMS by getting the user session from Auth.js. It doesn't handle the Auth.js stuff. First, you need to setup Auth.js before you can use this plugin. You can follow the [Auth.js guide](https://authjs.dev/getting-started/installation?framework=Next.js).
30+
### 1. Setup Auth.js
3131

32-
> ⚠ Make sure you define your config in a separate file (e.g. `auth.config.ts`) than where you create the NextAuth instance (e.g. `auth.ts`) to avoid circular dependencies. ⚠
32+
First of all, this plugin only integrates Auth.js into Payload CMS by getting the user session from Auth.js. It doesn't handle the Auth.js stuff. You need to setup Auth.js before you can use this plugin.
33+
34+
#### 1.1. Create your Auth.js configuration
35+
36+
Define your Auth.js configuration in a file (e.g. `auth.config.ts`):
3337

3438
```ts
3539
// auth.config.ts
@@ -43,9 +47,9 @@ export const authConfig: NextAuthConfig = {
4347
};
4448
```
4549

46-
Once you have configured Auth.js, you can integrate it with Payload CMS.
50+
#### 1.2. Create your Auth.js instance
4751

48-
To do this, wrap your Auth.js configuration with the `withPayload` function before creating the NextAuth instance:
52+
Next, create your Auth.js instance in a file (e.g. `auth.ts`). But before creating the Auth.js instance, you need to wrap your Auth.js configuration with the `withPayload` function:
4953

5054
```ts
5155
// auth.ts
@@ -61,7 +65,40 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
6165
);
6266
```
6367

64-
And add the `authjsPlugin` to your Payload configuration file:
68+
> ⚠ Make sure you define your config in a separate file than where you create the NextAuth instance to avoid circular dependencies.
69+
70+
#### 1.3. Add Auth.js route handler
71+
72+
Add the Auth.js route handler under `/app/api/auth/[...nextauth]/route.ts`:
73+
74+
```ts
75+
// app/api/auth/[...nextauth]/route.ts
76+
import { handlers } from "@/auth";
77+
78+
export const { GET, POST } = handlers;
79+
```
80+
81+
#### 1.4. Add Auth.js middleware (optional)
82+
83+
Add optional `middleware.ts` to keep the session alive, this will update the session expiry each time it's called.
84+
85+
> ⚠ Unlike what you would normally do in Auth.js, you cannot use the `middleware` of `@/auth` directly. You have to create a new Auth.js instance without the `withPayload` wrapper to be [edge-compatible](https://authjs.dev/guides/edge-compatibility).
86+
87+
```ts
88+
// middleware.ts
89+
import NextAuth from "next-auth";
90+
import { authConfig } from "./auth.config";
91+
92+
export const { auth: middleware } = NextAuth(authConfig);
93+
94+
export const config = {
95+
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|admin/login).*)"],
96+
};
97+
```
98+
99+
### 2. Add the payload-authjs plugin to Payload CMS
100+
101+
And finally, add the `authjsPlugin` to your Payload configuration file:
65102

66103
```ts
67104
// payload.config.ts

0 commit comments

Comments
 (0)