You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/payload-authjs/README.md
+42-5Lines changed: 42 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,9 +27,13 @@ Install the plugin using any JavaScript package manager such as PNPM, NPM, or Ya
27
27
pnpm i payload-authjs
28
28
```
29
29
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
31
31
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`):
Once you have configured Auth.js, you can integrate it with Payload CMS.
50
+
#### 1.2. Create your Auth.js instance
47
51
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:
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
+
exportconst { 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).
0 commit comments