Integrates Auth.js 5 (beta) into Payload CMS 3. The plugin creates a users collection, supplies a database adapter that stores Auth.js users in the Payload database, and registers a custom auth strategy so Payload reads the session from Auth.js. It requires an Auth.js configuration, a route handler in the Next.js app, and the Payload plugin entry in the config.
A Payload CMS 3 plugin for Auth.js 5
Installation
npm i payload-authjs206
Stars
10
Forks
1
Open Issues
1 weeks ago
Last Updated
3.5k
Weekly Downloads
v0.10.4
npm Version
273.7 KB
Unpacked Size
0
Dependencies
206+1 since Sep 7
| Date | Stars |
|---|---|
| Dec 6, 2026 | 206 |
| Dec 5, 2026 | 206 |
| Dec 4, 2026 | 206 |
| Dec 3, 2026 | 206 |
| Dec 2, 2026 | 206 |
| Dec 1, 2026 | 206 |
| Nov 30, 2026 | 206 |
| Nov 29, 2026 | 206 |
| Nov 28, 2026 | 206 |
| Nov 27, 2026 | 206 |
| Nov 26, 2026 | 206 |
| Nov 25, 2026 | 206 |
| Nov 24, 2026 | 206 |
| Nov 23, 2026 | 206 |
| Nov 22, 2026 | 206 |
| Nov 21, 2026 | 206 |
| Nov 20, 2026 | 206 |
| Nov 19, 2026 | 206 |
| Nov 18, 2026 | 206 |
| Nov 17, 2026 | 206 |
| Nov 16, 2026 | 206 |
| Nov 15, 2026 | 206 |
| Nov 14, 2026 | 206 |
| Nov 13, 2026 | 206 |
| Nov 12, 2026 | 206 |
| Nov 11, 2026 | 206 |
| Nov 10, 2026 | 206 |
| Nov 9, 2026 | 206 |
| Nov 8, 2026 | 206 |
| Nov 7, 2026 | 207 |
| Nov 6, 2026 | 207 |
| Nov 5, 2026 | 207 |
| Nov 4, 2026 | 207 |
| Nov 3, 2026 | 207 |
| Nov 2, 2026 | 207 |
| Nov 1, 2026 | 207 |
| Oct 31, 2026 | 207 |
| Oct 30, 2026 | 207 |
| Oct 29, 2026 | 207 |
| Oct 28, 2026 | 207 |
| Oct 27, 2026 | 207 |
| Oct 26, 2026 | 207 |
| Oct 25, 2026 | 207 |
| Oct 24, 2026 | 207 |
| Oct 23, 2026 | 207 |
| Oct 22, 2026 | 207 |
| Oct 21, 2026 | 206 |
| Oct 20, 2026 | 206 |
| Oct 19, 2026 | 206 |
| Oct 18, 2026 | 206 |
| Oct 17, 2026 | 206 |
| Oct 16, 2026 | 206 |
| Oct 15, 2026 | 206 |
| Oct 14, 2026 | 206 |
| Oct 13, 2026 | 206 |
| Oct 12, 2026 | 206 |
| Oct 11, 2026 | 206 |
| Oct 10, 2026 | 206 |
| Oct 9, 2026 | 206 |
| Oct 8, 2026 | 206 |
| Oct 7, 2026 | 206 |
| Oct 6, 2026 | 206 |
| Oct 5, 2026 | 206 |
| Oct 4, 2026 | 206 |
| Oct 3, 2026 | 206 |
| Oct 2, 2026 | 206 |
| Oct 1, 2026 | 206 |
| Sep 30, 2026 | 206 |
| Sep 29, 2026 | 206 |
| Sep 28, 2026 | 206 |
| Sep 27, 2026 | 206 |
| Sep 26, 2026 | 206 |
| Sep 25, 2026 | 206 |
| Sep 24, 2026 | 206 |
| Sep 23, 2026 | 206 |
| Sep 22, 2026 | 206 |
| Sep 21, 2026 | 206 |
| Sep 20, 2026 | 205 |
| Sep 19, 2026 | 205 |
| Sep 18, 2026 | 205 |
| Sep 17, 2026 | 205 |
| Sep 16, 2026 | 205 |
| Sep 15, 2026 | 205 |
| Sep 14, 2026 | 205 |
| Sep 13, 2026 | 205 |
| Sep 12, 2026 | 205 |
| Sep 11, 2026 | 205 |
| Sep 10, 2026 | 205 |
| Sep 9, 2026 | 205 |
| Sep 8, 2026 | 205 |
| Sep 7, 2026 | 205 |
3.5K−550 since Sep 7
| Date | Weekly downloads |
|---|---|
| Dec 6, 2026 | 3,505 |
| Dec 5, 2026 | 3,349 |
| Dec 4, 2026 | 3,267 |
| Dec 3, 2026 | 3,386 |
| Dec 2, 2026 | 3,559 |
| Dec 1, 2026 | 3,307 |
| Nov 30, 2026 | 3,182 |
| Nov 29, 2026 | 2,729 |
| Nov 28, 2026 | 2,910 |
| Nov 27, 2026 | 2,922 |
| Nov 26, 2026 | 2,943 |
| Nov 25, 2026 | 2,685 |
| Nov 24, 2026 | 2,535 |
| Nov 23, 2026 | 2,569 |
| Nov 22, 2026 | 2,656 |
| Nov 21, 2026 | 2,663 |
| Nov 20, 2026 | 2,773 |
| Nov 19, 2026 | 2,556 |
| Nov 18, 2026 | 3,034 |
| Nov 17, 2026 | 3,213 |
| Nov 16, 2026 | 3,673 |
| Nov 15, 2026 | 4,577 |
| Nov 14, 2026 | 5,808 |
| Nov 13, 2026 | 5,818 |
| Nov 12, 2026 | 5,808 |
| Nov 11, 2026 | 5,259 |
| Nov 10, 2026 | 4,980 |
| Nov 9, 2026 | 4,396 |
| Nov 8, 2026 | 3,476 |
| Nov 7, 2026 | 2,227 |
| Nov 6, 2026 | 2,230 |
| Nov 5, 2026 | 2,389 |
| Nov 4, 2026 | 2,551 |
| Nov 3, 2026 | 2,769 |
| Nov 2, 2026 | 3,274 |
| Nov 1, 2026 | 3,332 |
| Oct 31, 2026 | 3,551 |
| Oct 30, 2026 | 3,626 |
| Oct 29, 2026 | 3,168 |
| Oct 28, 2026 | 2,666 |
| Oct 27, 2026 | 2,636 |
| Oct 26, 2026 | 2,299 |
| Oct 25, 2026 | 2,139 |
| Oct 24, 2026 | 2,166 |
| Oct 23, 2026 | 1,572 |
| Oct 22, 2026 | 2,195 |
| Oct 21, 2026 | 2,694 |
| Oct 20, 2026 | 2,855 |
| Oct 19, 2026 | 2,917 |
| Oct 18, 2026 | 3,324 |
| Oct 17, 2026 | 4,027 |
| Oct 16, 2026 | 5,898 |
| Oct 15, 2026 | 6,004 |
| Oct 14, 2026 | 7,740 |
| Oct 13, 2026 | 8,414 |
| Oct 12, 2026 | 8,818 |
| Oct 11, 2026 | 8,566 |
| Oct 10, 2026 | 7,990 |
| Oct 9, 2026 | 7,087 |
| Oct 8, 2026 | 6,936 |
| Oct 7, 2026 | 5,368 |
| Oct 6, 2026 | 4,480 |
| Oct 5, 2026 | 4,024 |
| Oct 4, 2026 | 4,020 |
| Oct 3, 2026 | 3,658 |
| Oct 2, 2026 | 3,353 |
| Oct 1, 2026 | 3,414 |
| Sep 30, 2026 | 4,345 |
| Sep 29, 2026 | 4,302 |
| Sep 28, 2026 | 4,258 |
| Sep 27, 2026 | 5,068 |
| Sep 26, 2026 | 5,106 |
| Sep 25, 2026 | 4,796 |
| Sep 24, 2026 | 5,079 |
| Sep 23, 2026 | 3,734 |
| Sep 22, 2026 | 3,784 |
| Sep 21, 2026 | 4,350 |
| Sep 20, 2026 | 3,479 |
| Sep 19, 2026 | 3,717 |
| Sep 18, 2026 | 4,692 |
| Sep 17, 2026 | 4,352 |
| Sep 16, 2026 | 5,091 |
| Sep 15, 2026 | 6,076 |
| Sep 14, 2026 | 5,494 |
| Sep 13, 2026 | 5,597 |
| Sep 12, 2026 | 5,802 |
| Sep 11, 2026 | 5,001 |
| Sep 10, 2026 | 5,093 |
| Sep 9, 2026 | 5,153 |
| Sep 8, 2026 | 4,167 |
| Sep 7, 2026 | 4,055 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/848986894-payload-authjs)<a href="https://payloaddirectory.dev/plugins/848986894-payload-authjs"><img src="https://payloaddirectory.dev/badge/848986894-payload-authjs.svg" alt="Payload Directory"></a>README
A Payload CMS 3 plugin for integrating Auth.js 5 (beta).
[!CAUTION] ⚠ Auth.js is now part of Better Auth: It is unknown whether Auth.js 5 will ever be released as a stable version. Please consider this before using this plugin.
This plugin
users collection in Payload CMS.Install the plugin using any JavaScript package manager such as PNPM, NPM, or Yarn:
pnpm i payload-authjs
This plugin integrates Auth.js into Payload CMS by getting the user session from Auth.js. You need to setup Auth.js before you can use this plugin.
Define your Auth.js configuration in a file (e.g. auth.config.ts):
// auth.config.ts
import type { NextAuthConfig } from "next-auth";
import github from "next-auth/providers/github";
export const authConfig: NextAuthConfig = {
providers: [
github, // <-- Add your provider here
],
};
Next, create your Auth.js instance in a file (e.g. auth.ts).
⚠ But unlike what you would normally do in Auth.js, you need to create the Payload instance first and using the
getAuthjsInstancefunction to retrieve the Auth.js instance.
// auth.ts
import payloadConfig from "@payload-config";
import { getPayload } from "payload";
import { getAuthjsInstance } from "payload-authjs";
const payload = await getPayload({ config: payloadConfig });
export const { handlers, signIn, signOut, auth } = getAuthjsInstance(payload);
Add the Auth.js route handler under /app/api/auth/[...nextauth]/route.ts:
// app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
Add optional proxy.ts/middleware.ts to keep the session alive, this will update the session expiry each time it's called.
Since Next.js 16, the
middlewarehas been replaced with theproxyand uses the nodejs runtime. See the Next.js docs.
// proxy.ts
export { auth as proxy } from "./auth";
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|admin).*)"],
};
⚠ Unlike what you would normally do in Auth.js, you cannot use the
middlewareof@/authdirectly. You have to create a new Auth.js instance to be edge-compatible.
// middleware.ts
import NextAuth from "next-auth";
import { authConfig } from "./auth.config";
export const { auth: middleware } = NextAuth(authConfig);
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|admin).*)"],
};
And finally, add the authjsPlugin to your Payload configuration file:
// payload.config.ts
import { authjsPlugin } from "payload-authjs";
import { authConfig } from "./auth.config";
export const config = buildConfig({
plugins: [
authjsPlugin({
authjsConfig: authConfig,
}),
],
});
And that's it! Now you can sign-in via Auth.js and you are automatically authenticated in Payload CMS. Nice 🎉
You don't need to create a users collection. This plugin automatically creates a collection with the slug
users.
This plugin also provides some utility functions to get the current payload session/user within your Next.js application.
getPayloadSession)Instead of using the auth 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):
// ServerComponentExample.tsx
import { getPayloadSession } from "payload-authjs";
const ServerComponentExample = async () => {
const session = await getPayloadSession();
return (
<>
<h3>Payload CMS User:</h3>
<pre>{JSON.stringify(session?.user)}</pre>
</>
);
};
[!NOTE]
getPayloadSessionfetches the session from your server's/api/<collection>/meendpoint. The server URL is resolved from theNEXT_PUBLIC_SERVER_URL,AUTH_URLorNEXTAUTH_URLenvironment variable. If none of them is set, the URL is derived from the request headers (Host/X-Forwarded-Host), but only if the host is trusted (AUTH_TRUST_HOST, Vercel, Cloudflare Pages or a non-production environment). Otherwise an error is thrown.
usePayloadSession)Instead of using the useSession hook of Auth.js, you can use the usePayloadSession hook to get the current session in the client-side code:
Before you can use the usePayloadSession hook, you need to wrap your app with the PayloadSessionProvider:
// layout.tsx
import { PayloadSessionProvider } from "payload-authjs/client";
import { getPayloadSession } from "payload-authjs";
const Layout: React.FC<{ children: React.ReactNode }> = async ({ children }) => {
return (
<html lang="en">
<body>
<PayloadSessionProvider session={await getPayloadSession()}>
{children}
</PayloadSessionProvider>
</body>
</html>
);
};
export default Layout;
ℹ️ Passing the session to the
PayloadSessionProvideris optional, but it can be useful to avoid loading states.
You are now ready to use the usePayloadSession hook in your client-side code:
// ClientComponentExample.tsx
"use client";
import { usePayloadSession } from "payload-authjs/client";
export const ClientComponentExample = () => {
const { session } = usePayloadSession();
return (
<>
<h3>Payload CMS User:</h3>
<pre>{JSON.stringify(session?.user)}</pre>
</>
);
};
useAuth)If you want to access the current user in the Payload admin panel e.g. in a custom component. You can use the useAuth hook from Payload CMS:
"use client";
import { Banner, useAuth } from "@payloadcms/ui";
import type { User } from "@/payload-types";
export const CustomAdminComponent = () => {
const { user } = useAuth<User>();
if (!user) {
return null;
}
return <Banner type="success">Hi, {user.name}</Banner>;
};
req.user)Simply use the req.user object to determine the current user:
const Examples: CollectionConfig = {
slug: "examples",
access: {
read: ({ req: { user } }) => {
return Boolean(user) // <-- Check if the user is authenticated
},
},
fields: [
...
],
};
req.user)Simply use the req.user object to determine the current user:
const Examples: CollectionConfig = {
slug: "examples",
fields: [
...
],
endpoints: [
{
method: "get",
path: "/example",
handler: (req) => {
return Response.json(req.user); // <-- Return the user object
},
},
],
};
If you want to customize the users collection, you can create a collection with the slug users and add your customizations there.
// users.ts
const Users: CollectionConfig = {
slug: "users",
fields: [],
};
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.
// users.ts
const Users: CollectionConfig = {
slug: "users",
fields: [
{
name: "id",
type: "text",
label: "Identifier", // <-- Add a label to the id field
admin: {
hidden: true, // <-- Hide id field in admin panel
},
},
{
name: "accounts",
type: "array",
fields: [
{
name: "provider",
type: "text",
label: "Account Provider", // <-- Add label to provider field
},
],
},
],
};
You can also add additional fields to the users collection.
There are 2 ways to add additional fields. It depends on the data you want to store and your Auth.js session strategy (session.strategy).
If you want to store additional data in the database, you can add a new field to the users collection and extend your Auth.js provider to include the new field in the user.
For example, you could add a locale field to the users collection:
// users.ts
const Users: CollectionConfig = {
slug: "users",
fields: [
// Add custom field for 'locale'
{
name: "locale",
type: "text",
},
],
};
Next, you need to extend the user object returned by your Auth.js provider. You can do this as shown in this example:
// auth.config.ts
const authConfig: NextAuthConfig = {
providers: [
keycloak({
/**
* Add additional fields to the user on first sign in
*/
profile(profile) {
return {
// Default fields from keycloak provider
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture,
// Custom fields
locale: profile.locale, // <-- Add your custom field (e.g. get locale from the profile)
};
},
}),
],
...
};
⚠ 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
signInevent. (See Events)
If you are using the Auth.js jwt session strategy (it's the default), you can use a virtual field to add additional data that should not be stored in the database.
This plugin extracts the virtual fields from your Auth.js jwt session (if available) and adds them to the user object.
For example, you could add a roles field to the users collection:
// users.ts
const Users: CollectionConfig = {
slug: "users",
fields: [
// Add custom field for 'roles'
{
name: "roles",
type: "json",
virtual: true, // <-- Make the field virtual
admin: {
hidden: true,
},
},
],
};
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.
You can do this as shown in this example:
// auth.config.ts
const authConfig: NextAuthConfig = {
callbacks: {
jwt: ({ token, trigger }) => {
// Add the virtual field to the token only on signIn/signUp (jwt callback will be called multiple times)
if (trigger === "signIn" || trigger === "signUp") {
token.roles = ["example-role"]; // <-- Add your virtual field to the token
}
return token;
},
session: ({ session, token }) => {
if (token) {
session.user.roles = token.roles; // <-- Forward the virtual field from the token to the session
}
return session;
},
},
...
};
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.
More information about extending your session can be found in the Auth.js documentation.
Now you can access your custom field, e.g. in the access control operations or elsewhere:
const Examples: CollectionConfig = {
slug: "examples",
access: {
read: ({ req: { user } }) => {
return user?.roles?.includes("user") ?? false; // <-- Check if the user has the role "user"
},
},
fields: [
...
],
};
If you are using typescript you can declare your Auth.js user type as shown in the following example:
// auth.config.ts
import type { PayloadAuthjsUser } from "payload-authjs";
import type { User as PayloadUser } from "@/payload-types";
declare module "next-auth" {
interface User extends PayloadAuthjsUser<PayloadUser> {}
}
More information about typescript can be found in the Auth.js documentation.
Auth.js emits some events that you can listen to. This plugin extends the events with additional parameters such as the database adapter and the payload instance.
More information about the events can be found in the Auth.js documentation.
signIn EventThe 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:
// auth.config.ts
import { EnrichedAuthConfig } from "payload-authjs";
export const authConfig: EnrichedAuthConfig = {
providers: [github],
events: {
/**
* Update user 'name', 'email' and 'image' on every sign in
*/
signIn: async ({ adapter, user, profile }) => {
if (!user.id || !profile) {
return;
}
await adapter?.updateUser?.({
id: user.id,
name:
(profile as unknown as GitHubProfile).name ?? (profile as unknown as GitHubProfile).login,
email: profile.email ?? undefined,
image: (profile as unknown as GitHubProfile).avatar_url,
});
},
},
};
customers for general users and admins for payload admin panel)payload simple social login
Google and Microsoft social login for the Payload admin panel and auth collections
7119/wk3v3
payload oauth2
Generic OAuth2 sign-in for Payload CMS with support for providers such as Google, Zitadel and Apple
6913.0k/wk200v3
payload github auth
Sign in to the Payload admin with a GitHub App, with access rules by email, org or team
6021/wk0v4
payload auth plugin
Authentication plugin adding OAuth, OIDC, SSO and passkey sign-in to Payload CMS
594.7k/wk310v3
payload better auth
Integrate Better Auth into Payload CMS 3 with generated collections, API endpoints and admin login
3474v3
payload better auth
Replace Payload's built-in auth with Better Auth, including admin email OTP and session-based req.user
301v3