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
A [Payload CMS 3](https://payloadcms.com) plugin for integrating [Auth.js 5 (beta)](https://authjs.dev).
11
11
12
-
> ⚠ This plugin and Auth.js is in beta and may have some bugs. Please report any issues you find.
12
+
> ⚠ This plugin and Auth.js 5 is in beta and may have some bugs. Please report any issues you find.
13
+
14
+
# ⚡ How it works
15
+
16
+
This plugin
17
+
18
+
- creates a `users` collection in Payload CMS.
19
+
- use an custom [Database Adapter](https://authjs.dev/getting-started/database) for Auth.js to store the users in the Payload CMS database.
20
+
- use a custom [Auth Strategy](https://payloadcms.com/docs/authentication/custom-strategies) for Payload CMS to retrieve the session from Auth.js.
13
21
14
22
# ⚙️ Installation / Setup
15
23
16
-
Install the plugin using any JavaScript package manager like PNPM, NPM, or Yarn:
24
+
Install the plugin using any JavaScript package manager such as PNPM, NPM, or Yarn:
17
25
18
26
```bash
19
27
pnpm i payload-authjs
20
28
```
21
29
22
-
Fist of all, setup Auth.js like you would do in a Next.js application. You can follow the [Auth.js guide](https://authjs.dev/getting-started/installation?framework=Next.js).
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).
23
31
24
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. ⚠
**And that's it! Now you can sign-in via Auth.js and you are automatically authenticated in Payload CMS. Nice 🎉**
70
80
71
-
> You don't need to create a collection for users. This plugin automatically creates a collection with the slug `users`.
81
+
> _You don't need to create a users collection. This plugin automatically creates a collection with the slug `users`._
72
82
73
83
---
74
84
85
+
# 🪄 Getting the payload session
86
+
87
+
This plugin also provides some utility functions to get the current payload session/user within your Next.js application.
88
+
89
+
## Server side (`getPayloadSession`)
90
+
91
+
Instead of using the [`auth`](https://authjs.dev/getting-started/session-management/get-session?framework=next-js) function of Auth.js, you can use the `getPayloadSession` function to get the current session in the server-side code (e.g. in RSC or API routes):
Instead of using the [`useSession`](https://authjs.dev/getting-started/session-management/get-session?framework=next-js-client) hook of Auth.js, you can use the `usePayloadSession` hook to get the current session in the client-side code:
112
+
113
+
Before you can use the `usePayloadSession` hook, you need to wrap your app with the `PayloadSessionProvider` on the server-side:
If you want to access the current user in the Payload admin panel e.g. in a custom component. You can use the [`useAuth`](https://payloadcms.com/docs/admin/hooks#useauth) hook from Payload CMS:
You can customize the existing fields in the users collection by adding the field to the collection and modifying the field. The fields will be merged together.
237
+
You can customize the existing fields in the users collection by adding the field to the collection and modifying the field. The fields are merged together.
> ⚠ Keep in mind that Auth.js doesn't update the user after the first sign-in. If you want to update the user on every sign-in, you can use the `signIn` event. (See [Events](#events))
325
+
> ⚠ Note that Auth.js doesn't update the user after the first sign-in. If you want to update the user on every sign-in, you can use the `signIn` event. (See [Events](#events))
181
326
182
327
### 2. Virtual fields (jwt session only)
183
328
184
329
If you are using the Auth.js `jwt` session strategy (it's the default), you can use a [virtual field](https://payloadcms.com/docs/fields/overview#virtual-fields) to add additional data that should not be stored in the database.
185
-
This plugin extracts the virtual fields from your Auth.js jwt session and adds them to the user object.
330
+
This plugin extracts the virtual fields from your Auth.js jwt session (if available) and adds them to the user object.
186
331
187
332
For example, you could add a `roles` field to the users collection:
Next, you need to extend your Auth.js session with your field. You can do this as shown in this example:
352
+
This plugin can only get the virtual fields, if **they are included in the Auth.js session**. So you need to extend your Auth.js token and session with your field.
353
+
354
+
You can do this as shown in this example:
208
355
209
356
```ts
210
357
// auth.config.ts
211
358
const authConfig:NextAuthConfig= {
212
359
callbacks: {
213
360
jwt: ({ token, trigger }) => {
361
+
// Add the virtual field to the token only on signIn/signUp (jwt callback will be called multiple times)
214
362
if (trigger==="signIn"||trigger==="signUp") {
215
363
token.roles= ["example-role"]; // <-- Add your virtual field to the token
At this point, you can implement your own logic to extend the session. E.g. extract from profile, fetch from a server, or something else.
378
+
At this point, you can implement your own logic to extend the session. For example extract from profile, fetch from a server, or something else.
231
379
232
380
_More information about extending your session can be found in the [Auth.js documentation](https://authjs.dev/guides/extending-the-session)._
233
381
234
-
### Use custom fields
382
+
### Using custom fields
235
383
236
-
Now you could access your custom field, e.g. in the access control operations or somewhere else:
384
+
Now you can access your custom field, e.g. in the access control operations or elsewhere:
237
385
238
386
```ts
239
387
const Examples:CollectionConfig= {
@@ -269,7 +417,7 @@ _More information about typescript can be found in the [Auth.js documentation](h
269
417
270
418
## 🎉 Events
271
419
272
-
Auth.js emits some [events](https://authjs.dev/reference/nextjs#events) that you can listen to. This plugin extends the events with additional parameters like the `adapter` and `payload` instance.
420
+
Auth.js emits some [events](https://authjs.dev/reference/nextjs#events) that you can listen to. This plugin extends the events with additional parameters such as the `adapter` and `payload` instance.
273
421
274
422
_More information about the events can be found in the [Auth.js documentation](https://authjs.dev/reference/nextjs#events)._
275
423
@@ -284,7 +432,7 @@ The following events are available:
284
432
285
433
### `signIn` Event
286
434
287
-
The `signIn` event is emitted when a user successfully signs in. For example, you could use this event to update the user's name on every sign-in:
435
+
The `signIn` event is fired when a user successfully signs in. For example, you could use this event to update the user's name on every sign-in:
0 commit comments