Skip to content

Commit 2767a2f

Browse files
committed
ログインボタンを追加
1 parent 717f065 commit 2767a2f

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

app/accountMenu.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"use client";
2+
3+
import { authClient } from "@/lib/auth-client";
4+
5+
export const AccountMenu = () => {
6+
const { data: session, isPending } = authClient.useSession();
7+
8+
if (isPending) {
9+
return <div className="w-10 h-10 skeleton rounded-full"></div>;
10+
}
11+
12+
if (session) {
13+
return (
14+
<div className="dropdown dropdown-end">
15+
<label tabIndex={0} className="btn btn-ghost btn-circle avatar">
16+
<div className="w-8 h-8 rounded-full">
17+
<img
18+
src={
19+
session.user?.image ??
20+
`https://avatar.vercel.sh/${session.user?.name}`
21+
}
22+
alt="user avatar"
23+
/>
24+
</div>
25+
</label>
26+
<ul
27+
tabIndex={0}
28+
className="mt-3 z-[1] p-2 shadow menu menu-sm dropdown-content bg-base-100 rounded-box w-52"
29+
>
30+
<li>
31+
<a onClick={() => authClient.signOut()}>ログアウト</a>
32+
</li>
33+
</ul>
34+
</div>
35+
);
36+
}
37+
38+
return (
39+
<div className="dropdown dropdown-end">
40+
<label tabIndex={0} className="btn btn-ghost btn-circle avatar">
41+
<div className="w-8 h-8 rounded-full">
42+
<svg
43+
xmlns="http://www.w3.org/2000/svg"
44+
fill="none"
45+
viewBox="0 0 24 24"
46+
strokeWidth={1.5}
47+
stroke="currentColor"
48+
className="w-full h-full"
49+
>
50+
<path
51+
strokeLinecap="round"
52+
strokeLinejoin="round"
53+
d="M17.982 18.725A7.488 7.488 0 0 0 12 15.75a7.488 7.488 0 0 0-5.982 2.975m11.963 0a9 9 0 1 0-11.963 0m11.963 0A8.966 8.966 0 0 1 12 21a8.966 8.966 0 0 1-5.982-2.275M15 9.75a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
54+
/>
55+
</svg>
56+
</div>
57+
</label>
58+
<ul className="z-1 shadow-md dropdown-content bg-base-100 rounded-box menu w-64">
59+
<li className="menu-title">
60+
ログインすると、チャット履歴を保存し別のデバイスからもアクセスできるようになります。
61+
</li>
62+
<li>
63+
<button
64+
onClick={() => authClient.signIn.social({ provider: "github" })}
65+
>
66+
GitHub でログイン
67+
</button>
68+
</li>
69+
<li>
70+
<button
71+
onClick={() => authClient.signIn.social({ provider: "google" })}
72+
>
73+
Google でログイン
74+
</button>
75+
</li>
76+
</ul>
77+
</div>
78+
);
79+
};

app/lib/auth.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import { betterAuth } from "better-auth";
22
import { prismaAdapter } from "better-auth/adapters/prisma";
33
import { PrismaClient } from "@/generated/prisma/client";
4+
import { getCloudflareContext } from "@opennextjs/cloudflare";
45

56
const prisma = new PrismaClient();
67

78
export const auth = betterAuth({
89
database: prismaAdapter(prisma, {
910
provider: "postgresql",
1011
}),
11-
emailAndPassword: {
12-
enabled: true,
12+
socialProviders: {
13+
github: {
14+
clientId:
15+
process.env.GITHUB_CLIENT_ID ??
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
(getCloudflareContext().env as any).GITHUB_CLIENT_ID,
18+
clientSecret:
19+
process.env.GITHUB_CLIENT_SECRET ??
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
(getCloudflareContext().env as any).GITHUB_CLIENT_SECRET,
22+
},
23+
google: {
24+
clientId:
25+
process.env.GOOGLE_CLIENT_ID ??
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
27+
(getCloudflareContext().env as any).GOOGLE_CLIENT_ID,
28+
clientSecret:
29+
process.env.GOOGLE_CLIENT_SECRET ??
30+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31+
(getCloudflareContext().env as any).GOOGLE_CLIENT_SECRET,
32+
},
1333
},
1434
});

app/navbar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Link from "next/link";
2+
import { AccountMenu } from "./accountMenu";
23
import { ThemeToggle } from "./[docs_id]/themeToggle";
34
export function Navbar() {
45
return (
@@ -32,6 +33,7 @@ export function Navbar() {
3233
{/* サイドバーが常時表示されている場合のみ */}
3334
<Link href="/" className="flex-1 font-bold text-xl">my.code();</Link>
3435
<ThemeToggle />
36+
<AccountMenu />
3537
</div>
3638
</>
3739
);

app/sidebar.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { usePathname } from "next/navigation";
44
import useSWR, { Fetcher } from "swr";
55
import { splitMarkdown } from "./[docs_id]/splitMarkdown";
66
import { pagesList } from "./pagesList";
7+
import { AccountMenu } from "./accountMenu";
78
import { ThemeToggle } from "./[docs_id]/themeToggle";
89

910
const fetcher: Fetcher<string, string> = (url) =>
@@ -20,12 +21,13 @@ export function Sidebar() {
2021
return (
2122
<div className="bg-base-200 h-full w-80 overflow-y-auto">
2223
{/* todo: 背景色ほんとにこれでいい? */}
23-
<h2 className="hidden lg:flex flex-row items-center p-4">
24+
<h2 className="hidden lg:flex flex-row items-center p-4 gap-2">
2425
{/* サイドバーが常時表示されている場合のみ */}
2526
<Link href="/" className="flex-1 text-xl font-bold">
2627
my.code();
2728
</Link>
2829
<ThemeToggle />
30+
<AccountMenu />
2931
</h2>
3032
<span className="block lg:hidden p-4 pb-0">
3133
<label

0 commit comments

Comments
 (0)