Skip to content

Commit 5f37620

Browse files
committed
sign in from client-side
1 parent a458300 commit 5f37620

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

app/routes/_index.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
import { Form, Link } from "react-router";
1+
import { Link, useFetcher } from "react-router";
2+
import { authClient } from "@/lib/auth-client";
23
import github from "/github.svg";
34
import logo from "/logo.svg";
45

56
export default function Page() {
7+
const fetcher = useFetcher();
8+
async function handlePlay() {
9+
const session = await authClient.getSession();
10+
if (!session?.data?.user) {
11+
await authClient.signIn.anonymous();
12+
}
13+
fetcher.submit(null, { method: "post", action: "/api/games" });
14+
}
615
return (
716
<div className="h-screen w-screen bg-[#1A472A] font-serif text-white relative flex justify-center">
817
<h1 className="absolute top-1/3 text-center font-bold text-4xl tracking-widest">
918
一人麻雀
1019
</h1>
1120
<div className="absolute top-1/2 flex flex-col md:flex-row items-center space-y-4 md:space-x-4 md:space-y-0">
12-
<Form method="post" action="/api/games" className="w-full md:w-30">
13-
<button
14-
type="submit"
15-
className="bg-yellow-600 rounded text-sm w-full h-10 transition-transform duration-150 hover:scale-105 text-white"
16-
>
17-
プレイ
18-
</button>
19-
</Form>
21+
<button
22+
onClick={handlePlay}
23+
type="button"
24+
className="bg-yellow-600 rounded text-sm w-full h-10 transition-transform duration-150 hover:scale-105 text-white"
25+
>
26+
プレイ
27+
</button>
2028
<Link
2129
to="/learn"
2230
className="bg-yellow-600 rounded text-sm w-full md:w-30 h-10 flex items-center justify-center transition-transform duration-150 hover:scale-105 text-white"

app/routes/api.games.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ export async function action({ context, request }: Route.LoaderArgs) {
88
const { env } = context.cloudflare;
99
const db = getDB(env);
1010
const auth = getAuth(env);
11-
12-
let session = await auth.api.getSession({ headers: request.headers });
11+
const session = await auth.api.getSession({ headers: request.headers });
1312
if (!session?.user.id) {
14-
await auth.api.signInAnonymous({ headers: request.headers });
15-
session = await auth.api.getSession({ headers: request.headers });
16-
if (!session?.user.id) {
17-
throw new Response("Unauthorized", { status: 401 });
18-
}
13+
throw new Response("Unauthorized", { status: 401 });
1914
}
2015
const userId = session.user.id;
2116
const randomHaiyama = await getRandomHaiyama(db, userId);

0 commit comments

Comments
 (0)