Skip to content

Commit f8fbe5f

Browse files
committed
anonymousプラグインを追加
1 parent 2767a2f commit f8fbe5f

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ https://my-code.utcode.net
55
## インストール
66
```bash
77
npm ci
8+
npx prisma generate
89
```
910

1011
ルートディレクトリに .env.local という名前のファイルを作成し、以下の内容を記述
@@ -13,13 +14,17 @@ API_KEY=GeminiAPIキー
1314
BETTER_AUTH_URL=http://localhost:3000
1415
```
1516

17+
<!-- 本番環境では BETTER_AUTH_SECRET と DATABASE_URL の設定も必要です。 -->
18+
1619
prismaの開発環境を起動
1720
(.env にDATABASE_URLが自動的に追加される)
1821
```bash
1922
npx prisma dev
2023
```
21-
22-
<!-- 本番環境では BETTER_AUTH_SECRET と DATABASE_URL の設定も必要です。 -->
24+
別ターミナルで
25+
```bash
26+
npx prisma db push
27+
```
2328

2429
## 開発環境
2530

app/accountMenu.tsx

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

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

5-
export const AccountMenu = () => {
6+
export function AutoAnonymousLogin() {
7+
const { data: session, isPending } = authClient.useSession();
8+
useEffect(() => {
9+
if (!isPending && !session) {
10+
authClient.signIn.anonymous();
11+
}
12+
}, [isPending, session]);
13+
14+
return null;
15+
}
16+
17+
export function AccountMenu() {
618
const { data: session, isPending } = authClient.useSession();
719

820
if (isPending) {
921
return <div className="w-10 h-10 skeleton rounded-full"></div>;
1022
}
1123

12-
if (session) {
24+
if (session && !session.user.isAnonymous) {
1325
return (
1426
<div className="dropdown dropdown-end">
1527
<label tabIndex={0} className="btn btn-ghost btn-circle avatar">
@@ -76,4 +88,4 @@ export const AccountMenu = () => {
7688
</ul>
7789
</div>
7890
);
79-
};
91+
}

app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ReactNode } from "react";
88
import { PyodideProvider } from "./terminal/python/pyodide";
99
import { WandboxProvider } from "./terminal/wandbox/wandbox";
1010
import { EmbedContextProvider } from "./terminal/embedContext";
11+
import { AutoAnonymousLogin } from "./accountMenu";
1112

1213
export const metadata: Metadata = {
1314
title: "Create Next App",
@@ -20,6 +21,7 @@ export default function RootLayout({
2021
return (
2122
<html lang="ja">
2223
<body className="w-screen h-screen">
24+
<AutoAnonymousLogin />
2325
<div className="drawer lg:drawer-open">
2426
<input id="drawer-toggle" type="checkbox" className="drawer-toggle" />
2527
<div className="drawer-content flex flex-col">

app/lib/auth-client.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { createAuthClient } from "better-auth/react"
1+
import { anonymousClient } from "better-auth/client/plugins";
2+
import { createAuthClient } from "better-auth/react";
23
export const authClient = createAuthClient({
3-
/** The base URL of the server (optional if you're using the same domain) */
4-
// baseURL: "http://localhost:3000"
5-
})
4+
/** The base URL of the server (optional if you're using the same domain) */
5+
// baseURL: "http://localhost:3000"
6+
plugins: [anonymousClient()],
7+
});

app/lib/auth.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
import { betterAuth } from "better-auth";
22
import { prismaAdapter } from "better-auth/adapters/prisma";
3-
import { PrismaClient } from "@/generated/prisma/client";
3+
import { PrismaClient } from "../generated/prisma/client";
44
import { getCloudflareContext } from "@opennextjs/cloudflare";
5+
import { anonymous } from "better-auth/plugins";
56

67
const prisma = new PrismaClient();
78

9+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10+
let cloudflareEnv: any;
11+
try {
12+
cloudflareEnv = getCloudflareContext().env;
13+
} catch {
14+
// @better-auth/cli generate を実行する際には initOpenNextCloudflareForDev がセットアップされていない環境になっている
15+
cloudflareEnv = {};
16+
}
817
export const auth = betterAuth({
918
database: prismaAdapter(prisma, {
1019
provider: "postgresql",
1120
}),
21+
plugins: [
22+
anonymous({
23+
onLinkAccount: async ({ anonymousUser, newUser }) => {
24+
// TODO
25+
},
26+
}),
27+
],
1228
socialProviders: {
1329
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,
30+
clientId: process.env.GITHUB_CLIENT_ID ?? cloudflareEnv.GITHUB_CLIENT_ID,
1831
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,
32+
process.env.GITHUB_CLIENT_SECRET ?? cloudflareEnv.GITHUB_CLIENT_SECRET,
2233
},
2334
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,
35+
clientId: process.env.GOOGLE_CLIENT_ID ?? cloudflareEnv.GOOGLE_CLIENT_ID,
2836
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,
37+
process.env.GOOGLE_CLIENT_SECRET ?? cloudflareEnv.GOOGLE_CLIENT_SECRET,
3238
},
3339
},
3440
});

prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ model User {
2525
sessions Session[]
2626
accounts Account[]
2727
28+
isAnonymous Boolean?
29+
2830
@@unique([email])
2931
@@map("user")
3032
}

0 commit comments

Comments
 (0)