Skip to content

Commit a3bd556

Browse files
committed
fix(SignInButton): Mount authjs SessionProvider to the admin panel to provide custom base path (#44)
1 parent e69bfbe commit a3bd556

File tree

5 files changed

+65
-39
lines changed

5 files changed

+65
-39
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use client";
2+
3+
import { SessionProvider, signIn } from "next-auth/react";
4+
5+
export function SignInButtonClient() {
6+
return (
7+
<SessionProvider basePath="/api/auth/customers">
8+
<button onClick={() => signIn()}>Sign In (Customer)</button>
9+
</SessionProvider>
10+
);
11+
}

examples/multiple-auth-collections/src/app/(app)/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { auth } from "@/auth.customers";
22
import { getPayloadSession } from "payload-authjs";
3-
import { SignInButton } from "./_components/SignInButton";
3+
import { SignInButtonClient } from "./_components/SignInButtonClient";
44
import { SignOutButton } from "./_components/SignOutButton";
55

66
const Page = async () => {
@@ -9,7 +9,7 @@ const Page = async () => {
99

1010
return (
1111
<main>
12-
{payloadSession ? <SignOutButton /> : <SignInButton />}
12+
{payloadSession ? <SignOutButton /> : <SignInButtonClient />}
1313
<br />
1414
<h3>Auth.js Session (Customer)</h3>
1515
<pre>{JSON.stringify(authjsSession, null, 2)}</pre>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { AccountRowLabel as AccountRowLabel_06d0cb594d8f6ba2ac35015f930c882e } from 'payload-authjs/components'
22
import { SignInButton as SignInButton_06d0cb594d8f6ba2ac35015f930c882e } from 'payload-authjs/components'
3+
import { SessionProvider as SessionProvider_eb65d9ad4ff5e9757fd28d232b1c2581 } from 'next-auth/react'
34

45
export const importMap = {
56
"payload-authjs/components#AccountRowLabel": AccountRowLabel_06d0cb594d8f6ba2ac35015f930c882e,
6-
"payload-authjs/components#SignInButton": SignInButton_06d0cb594d8f6ba2ac35015f930c882e
7+
"payload-authjs/components#SignInButton": SignInButton_06d0cb594d8f6ba2ac35015f930c882e,
8+
"next-auth/react#SessionProvider": SessionProvider_eb65d9ad4ff5e9757fd28d232b1c2581
79
}

packages/payload-authjs/src/payload/plugin.ts

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NextAuthConfig } from "next-auth";
2-
import type { CollectionSlug, Plugin } from "payload";
2+
import type { CollectionSlug, CustomComponent, Plugin } from "payload";
33
import { getProviderMetadata } from "../authjs/utils/config";
44
import type { SignInButtonOptions, SignInButtonProps } from "../components/SignInButton";
55
import { generateUsersCollection } from "./collection";
@@ -63,43 +63,56 @@ export const authjsPlugin =
6363
config.collections = config.collections ?? [];
6464
generateUsersCollection(config.collections, pluginOptions);
6565

66-
// Add custom components to admin
67-
config.admin = {
68-
...config.admin,
69-
components: {
70-
...config.admin?.components,
71-
afterLogin: [
72-
...(config.admin?.components?.afterLogin ?? []),
73-
// Add the SignInButton component to the admin login page (only if the user collection is the admin user collection)
74-
...(() => {
75-
if (incomingConfig.admin?.user !== (pluginOptions.userCollectionSlug ?? "users")) {
76-
return [];
77-
}
78-
if (pluginOptions.components?.SignInButton === false) {
79-
return [];
80-
}
81-
const signInButtonOptions = pluginOptions.components?.SignInButton;
82-
return pluginOptions.authjsConfig.providers
66+
// Add the SignInButton component to the admin login page (only if the user collection is the admin user collection)
67+
if (
68+
incomingConfig.admin?.user === (pluginOptions.userCollectionSlug ?? "users") &&
69+
pluginOptions.components?.SignInButton !== false
70+
) {
71+
const signInButtonOptions = pluginOptions.components?.SignInButton;
72+
config.admin = {
73+
...config.admin,
74+
components: {
75+
...config.admin?.components,
76+
providers: [
77+
...(config.admin?.components?.providers ?? []),
78+
// Add the Auth.js SessionProvider to set the custom basePath
79+
...(pluginOptions.authjsConfig.basePath
80+
? [
81+
{
82+
path: "next-auth/react#SessionProvider",
83+
clientProps: {
84+
basePath: pluginOptions.authjsConfig.basePath,
85+
},
86+
},
87+
]
88+
: []),
89+
],
90+
afterLogin: [
91+
...(config.admin?.components?.afterLogin ?? []),
92+
...pluginOptions.authjsConfig.providers
8393
.map(provider => getProviderMetadata(provider))
8494
.filter(provider => ["oauth", "oidc"].includes(provider.type))
85-
.map(provider => ({
86-
path: "payload-authjs/components#SignInButton",
87-
clientProps: {
88-
icon:
89-
typeof signInButtonOptions?.icon === "function"
90-
? signInButtonOptions.icon(provider)
91-
: signInButtonOptions?.icon,
92-
text:
93-
typeof signInButtonOptions?.text === "function"
94-
? signInButtonOptions.text(provider)
95-
: signInButtonOptions?.text,
96-
provider,
97-
} satisfies SignInButtonProps,
98-
}));
99-
})(),
100-
],
101-
},
102-
};
95+
.map(
96+
provider =>
97+
({
98+
path: "payload-authjs/components#SignInButton",
99+
clientProps: {
100+
icon:
101+
typeof signInButtonOptions?.icon === "function"
102+
? signInButtonOptions.icon(provider)
103+
: signInButtonOptions?.icon,
104+
text:
105+
typeof signInButtonOptions?.text === "function"
106+
? signInButtonOptions.text(provider)
107+
: signInButtonOptions?.text,
108+
provider,
109+
} satisfies SignInButtonProps,
110+
}) satisfies CustomComponent,
111+
),
112+
],
113+
},
114+
};
115+
}
103116

104117
return config;
105118
};

0 commit comments

Comments
 (0)