Skip to content

Commit c24eb72

Browse files
committed
feat: Add sign in with auth.js button to admin login page
1 parent 47911bf commit c24eb72

File tree

6 files changed

+83
-3
lines changed

6 files changed

+83
-3
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
1+
import { SignInWithAuthjsButton as SignInWithAuthjsButton_0 } from 'payload-authjs/components'
22

33
export const importMap = {
4-
4+
"payload-authjs/components#SignInWithAuthjsButton": SignInWithAuthjsButton_0
55
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"types": "./dist/middleware.d.ts",
1717
"import": "./dist/middleware.js",
1818
"default": "./dist/middleware.js"
19+
},
20+
"./components": {
21+
"types": "./dist/components/index.d.ts",
22+
"import": "./dist/components/index.js",
23+
"default": "./dist/components/index.js"
1924
}
2025
},
2126
"files": [
@@ -86,6 +91,8 @@
8691
"typescript": "^5.6.2"
8792
},
8893
"nx": {},
89-
"dependencies": {},
94+
"dependencies": {
95+
"@auth/core": "^0.34.2"
96+
},
9097
"packageManager": "pnpm@9.9.0"
9198
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createActionURL } from "@auth/core";
2+
import { Button } from "@payloadcms/ui";
3+
import { headers } from "next/headers";
4+
import { redirect } from "next/navigation";
5+
import React from "react";
6+
7+
/**
8+
* A button that redirects the user to the Auth.js sign in page
9+
*/
10+
export const SignInWithAuthjsButton = ({
11+
authjsBasePath,
12+
adminURL,
13+
}: {
14+
authjsBasePath: string;
15+
adminURL: string;
16+
}) => {
17+
return (
18+
<form
19+
style={{ display: "flex", justifyContent: "center" }}
20+
action={async () => {
21+
"use server";
22+
23+
const signInURL = createActionURL(
24+
"signin",
25+
"https",
26+
headers(),
27+
process.env,
28+
authjsBasePath,
29+
);
30+
signInURL.searchParams.append("callbackUrl", adminURL);
31+
32+
redirect(signInURL.toString());
33+
}}
34+
>
35+
<Button
36+
type="submit"
37+
size="large"
38+
buttonStyle="secondary"
39+
icon={<img src="https://authjs.dev/img/logo-sm.png" alt="Auth.js Logo" />}
40+
iconPosition="left"
41+
>
42+
Sign in with Auth.js
43+
</Button>
44+
</form>
45+
);
46+
};

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { SignInWithAuthjsButton } from "./SignInWithAuthjsButton";

src/plugin.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,28 @@ export const authjsPlugin =
1414
config.collections = config.collections ?? [];
1515
generateUsersCollection(config.collections, pluginOptions);
1616

17+
// Add custom components to admin
18+
config.admin = {
19+
...config.admin,
20+
components: {
21+
...config.admin?.components,
22+
afterLogin: [
23+
...(config.admin?.components?.afterLogin ?? []),
24+
// Add the SignInWithAuthjsButton component to the admin login page (only if the user collection is the admin user collection)
25+
...(incomingConfig.admin?.user === (pluginOptions.userCollectionSlug ?? "users")
26+
? [
27+
{
28+
path: "payload-authjs/components#SignInWithAuthjsButton",
29+
serverProps: {
30+
authjsBasePath: pluginOptions.authjsConfig.basePath ?? "/api/auth",
31+
adminURL: config.routes?.admin ?? "/admin",
32+
},
33+
},
34+
]
35+
: []),
36+
],
37+
},
38+
};
39+
1740
return config;
1841
};

0 commit comments

Comments
 (0)