Skip to content

Commit 3f3cb6f

Browse files
committed
ソーシャルログインとログアウトの挙動を実装
1 parent 62960eb commit 3f3cb6f

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ API_KEY=GeminiAPIキー
1414
BETTER_AUTH_URL=http://localhost:3000
1515
```
1616

17-
<!-- 本番環境では BETTER_AUTH_SECRET と DATABASE_URL の設定も必要です。 -->
18-
1917
prismaの開発環境を起動
2018
(.env にDATABASE_URLが自動的に追加される)
2119
```bash
@@ -26,6 +24,15 @@ npx prisma dev
2624
npx prisma db push
2725
```
2826

27+
### 本番環境の場合
28+
29+
上記の環境変数以外に、
30+
* BETTER_AUTH_SECRET に任意の文字列
31+
* DATABASE_URL に本番用のPostgreSQLデータベースURL
32+
* GOOGLE_CLIENT_IDとGOOGLE_CLIENT_SECRETにGoogle OAuthのクライアントIDとシークレット https://www.better-auth.com/docs/authentication/google
33+
* GITHUB_CLIENT_IDとGITHUB_CLIENT_SECRETにGitHub OAuthのクライアントIDとシークレット https://www.better-auth.com/docs/authentication/github
34+
35+
2936
## 開発環境
3037

3138
```bash

app/accountMenu.tsx

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { authClient } from "@/lib/auth-client";
4+
import { usePathname } from "next/navigation";
45
import { useEffect } from "react";
56

67
export function AutoAnonymousLogin() {
@@ -16,6 +17,30 @@ export function AutoAnonymousLogin() {
1617

1718
export function AccountMenu() {
1819
const { data: session, isPending } = authClient.useSession();
20+
const pathname = usePathname();
21+
22+
const signout = () => {
23+
if (
24+
window.confirm(
25+
"ログアウトしますか?\nチャット履歴はこの端末上で見られなくなりますが、再度ログインすることでアクセスできます。"
26+
)
27+
) {
28+
authClient.signOut({
29+
fetchOptions: {
30+
onSuccess: () => window.location.reload(),
31+
},
32+
});
33+
}
34+
};
35+
const signoutFromAnonymous = () => {
36+
if (window.confirm("チャット履歴は削除され、アクセスできなくなります。")) {
37+
authClient.signOut({
38+
fetchOptions: {
39+
onSuccess: () => window.location.reload(),
40+
},
41+
});
42+
}
43+
};
1944

2045
if (isPending) {
2146
return <div className="w-10 h-10 skeleton rounded-full"></div>;
@@ -40,7 +65,7 @@ export function AccountMenu() {
4065
className="mt-3 z-[1] p-2 shadow menu menu-sm dropdown-content bg-base-100 rounded-box w-52"
4166
>
4267
<li>
43-
<a onClick={() => authClient.signOut()}>ログアウト</a>
68+
<a onClick={signout}>ログアウト</a>
4469
</li>
4570
</ul>
4671
</div>
@@ -73,18 +98,38 @@ export function AccountMenu() {
7398
</li>
7499
<li>
75100
<button
76-
onClick={() => authClient.signIn.social({ provider: "github" })}
101+
onClick={() =>
102+
authClient.signIn.social({
103+
provider: "github",
104+
callbackURL: pathname,
105+
})
106+
}
77107
>
78108
GitHub でログイン
79109
</button>
80110
</li>
81111
<li>
82112
<button
83-
onClick={() => authClient.signIn.social({ provider: "google" })}
113+
onClick={() =>
114+
authClient.signIn.social({
115+
provider: "google",
116+
callbackURL: pathname,
117+
})
118+
}
84119
>
85120
Google でログイン
86121
</button>
87122
</li>
123+
{session?.user && (
124+
<>
125+
<div className="divider my-0" />
126+
<li>
127+
<button onClick={signoutFromAnonymous}>
128+
この端末上のデータを削除
129+
</button>
130+
</li>
131+
</>
132+
)}
88133
</ul>
89134
</div>
90135
);

0 commit comments

Comments
 (0)