|
1 | 1 | import type { NextAuthConfig } from "next-auth"; |
2 | | -import type { CollectionSlug, Plugin } from "payload"; |
| 2 | +import type { CollectionSlug, CustomComponent, Plugin } from "payload"; |
3 | 3 | import { getProviderMetadata } from "../authjs/utils/config"; |
4 | 4 | import type { SignInButtonOptions, SignInButtonProps } from "../components/SignInButton"; |
5 | 5 | import { generateUsersCollection } from "./collection"; |
@@ -63,43 +63,56 @@ export const authjsPlugin = |
63 | 63 | config.collections = config.collections ?? []; |
64 | 64 | generateUsersCollection(config.collections, pluginOptions); |
65 | 65 |
|
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 |
83 | 93 | .map(provider => getProviderMetadata(provider)) |
84 | 94 | .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 | + } |
103 | 116 |
|
104 | 117 | return config; |
105 | 118 | }; |
0 commit comments