|
| 1 | +# Payload CMS plugin for Auth.js/NextAuth |
| 2 | + |
| 3 | +<a href="https://github.com/CrawlerCode/payload-authjs/actions/workflows/ci.yml"><img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/CrawlerCode/payload-authjs/ci.yml?style=flat-square&logo=github"></a> |
| 4 | +<a href="https://www.npmjs.com/package/payload-authjs"><img alt="NPM Version" src="https://img.shields.io/npm/v/payload-authjs?style=flat-square"></a> |
| 5 | +<a href="https://github.com/CrawlerCode/payload-authjs/blob/main/LICENSE"><img alt="NPM License" src="https://img.shields.io/npm/l/payload-authjs?style=flat-square"></a> |
| 6 | +<a href="https://www.npmjs.com/package/payload-authjs"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/payload-authjs?style=flat-square"></a> |
| 7 | + |
| 8 | +A [Payload CMS 3](https://payloadcms.com) plugin for integrating [Auth.js 5 (beta)](https://authjs.dev). |
| 9 | + |
| 10 | +> ⚠ This plugin and Auth.js is in beta and may have some bugs. Please report any issues you find. |
| 11 | +
|
| 12 | +## Installation |
| 13 | + |
| 14 | +Install the plugin using any JavaScript package manager like PNPM, NPM, or Yarn: |
| 15 | + |
| 16 | +```bash |
| 17 | +pnpm i payload-authjs |
| 18 | +``` |
| 19 | + |
| 20 | +Fist of all, setup Auth.js like you would do in a Next.js application. You can follow the [Auth.js documentation](https://authjs.dev/getting-started/installation?framework=Next.js). |
| 21 | + |
| 22 | +> ⚠ 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. ⚠ |
| 23 | +
|
| 24 | +```ts |
| 25 | +// auth.config.ts |
| 26 | +import github from "next-auth/providers/github"; |
| 27 | + |
| 28 | +export const authConfig: NextAuthConfig = { |
| 29 | + providers: [ |
| 30 | + github, // <-- Add your provider here |
| 31 | + ], |
| 32 | +}; |
| 33 | +``` |
| 34 | + |
| 35 | +Wrap your Auth.js configuration with the `withPayload` function before creating the NextAuth instance: |
| 36 | + |
| 37 | +```ts |
| 38 | +// auth.ts |
| 39 | +import payloadConfig from "@payload-config"; |
| 40 | +import NextAuth from "next-auth"; |
| 41 | +import { withPayload } from "payload-authjs"; |
| 42 | +import { authConfig } from "./auth.config"; // ⚠ Import the config from a separate file |
| 43 | + |
| 44 | +export const { handlers, signIn, signOut, auth } = NextAuth( |
| 45 | + withPayload(authConfig, { |
| 46 | + payloadConfig, |
| 47 | + }), |
| 48 | +); |
| 49 | +``` |
| 50 | + |
| 51 | +Add the `authjsPlugin` in your Payload configuration file: |
| 52 | + |
| 53 | +```ts |
| 54 | +// payload.config.ts |
| 55 | +import { authjsPlugin } from "payload-authjs"; |
| 56 | +import { authConfig } from "./auth.config"; |
| 57 | + |
| 58 | +export const config = buildConfig({ |
| 59 | + plugins: [ |
| 60 | + authjsPlugin({ |
| 61 | + authjsConfig: authConfig, |
| 62 | + }), |
| 63 | + ], |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +**And that's it! Now you can sign-in via Auth.js and you are automatically authenticated in Payload CMS. Nice 🎉** |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Customizing |
| 72 | + |
| 73 | +You don't need to create a collection for users. This plugin automatically creates a collection with the slug `users`. |
| 74 | + |
| 75 | +But if you want to customize the users collection, you can create a collection with the slug `users` and add the fields you need. |
| 76 | + |
| 77 | +```ts |
| 78 | +// users.ts |
| 79 | +import type { CollectionConfig } from "payload"; |
| 80 | + |
| 81 | +const Users: CollectionConfig = { |
| 82 | + slug: "users", |
| 83 | + fields: [ |
| 84 | + { |
| 85 | + name: "roles", |
| 86 | + type: "json", |
| 87 | + }, |
| 88 | + ], |
| 89 | +}; |
| 90 | + |
| 91 | +export default Users; |
| 92 | +``` |
| 93 | + |
| 94 | +Next, you need to extend the user object returned by your Auth.js provider. You can do this like this example: |
| 95 | + |
| 96 | +```ts |
| 97 | +const authConfig: NextAuthConfig = { |
| 98 | + providers: [ |
| 99 | + github({ |
| 100 | + profile(profile) { |
| 101 | + return { |
| 102 | + id: profile.id.toString(), |
| 103 | + name: profile.name, |
| 104 | + email: profile.email, |
| 105 | + image: profile.avatar_url, |
| 106 | + roles: ["user"], // <-- Extend the user object with a custom field |
| 107 | + }; |
| 108 | + }, |
| 109 | + }), |
| 110 | + ], |
| 111 | + ... |
| 112 | +}; |
| 113 | +``` |
| 114 | + |
| 115 | +⚠ 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 `updateUserOnSignIn` option in the `withPayload` function: |
| 116 | + |
| 117 | +```ts |
| 118 | +// auth.ts |
| 119 | +export const { handlers, signIn, signOut, auth } = NextAuth( |
| 120 | + withPayload(authConfig, { |
| 121 | + payloadConfig, |
| 122 | + updateUserOnSignIn: true, // <-- Update the user on every sign-in |
| 123 | + }), |
| 124 | +); |
| 125 | +``` |
| 126 | + |
| 127 | +Now you could access your custom field, e.g. in the access control operations: |
| 128 | + |
| 129 | +```ts |
| 130 | +const Examples: CollectionConfig = { |
| 131 | + slug: "examples", |
| 132 | + access: { |
| 133 | + read: ({ req: { user } }) => { |
| 134 | + return user?.roles?.includes("user") ?? false; // <-- Check if the user has the role "user" |
| 135 | + }, |
| 136 | + }, |
| 137 | + fields: [ |
| 138 | + ... |
| 139 | + ], |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +### Utility functions |
| 144 | + |
| 145 | +This plugin also export a utility function to get the current payload user |
| 146 | + |
| 147 | +```tsx |
| 148 | +// ServerComponentExample.tsx |
| 149 | +const ServerComponentExample = async () => { |
| 150 | + const payloadUser = await getPayloadUser(); |
| 151 | + |
| 152 | + return ( |
| 153 | + <div> |
| 154 | + <h3>Payload CMS User</h3> |
| 155 | + <div>{JSON.stringify(payloadUser)}</div> |
| 156 | + </div> |
| 157 | + ); |
| 158 | +}; |
| 159 | +``` |
0 commit comments