Skip to content

Commit 3584c54

Browse files
committed
simplify impl
1 parent a366cdd commit 3584c54

File tree

5 files changed

+14
-74
lines changed

5 files changed

+14
-74
lines changed

packages/web/src/components/FilterUI/FilterUI.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
* 全てのフィルターを表示するコンポーネント
33
*/
44

5-
import type {
6-
ClassSeries,
7-
ClassType,
8-
Evaluation,
9-
Semester,
10-
} from "@packages/models";
5+
import type { ClassSeries, Evaluation, Semester } from "@packages/models";
116
import { useState } from "react";
127
import { ClassSeriesFilter } from "./FilterComponents/ClassType.tsx";
138
import { EvaluationFilter } from "./FilterComponents/Evaluation.tsx";
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
import { createAuthClient } from "better-auth/react";
2-
import { writable } from "svelte/store";
32

43
export const auth = createAuthClient({
54
baseURL: window.location.origin,
65
});
76

8-
export const onAuthChange = writable({ loggedIn: false });
9-
107
export async function logout() {
118
await auth.signOut();
12-
onAuthChange.set({ loggedIn: false });
139
}
1410

1511
export async function signInWithGoogle(callbackURL: string) {
1612
await auth.signIn.social({
1713
provider: "google",
1814
callbackURL,
1915
});
20-
21-
onAuthChange.set({ loggedIn: true });
2216
}
23-
24-
(async () => {
25-
const session = await auth.getSession();
26-
console.log("[auth debug] ", session);
27-
if (session.data) {
28-
onAuthChange.set({ loggedIn: true });
29-
}
30-
})();

packages/web/src/pages/Profile.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { useCurrentUserQuery } from "@/services/user/user";
55
export default function Profile() {
66
const user = useCurrentUserQuery();
77

8-
if (!user.data) {
8+
if (user.isPending) {
99
return <div>Loading...</div>;
1010
}
1111

12-
if (!user.data.ok) {
12+
if (!user.data) {
1313
return (
1414
<div>
1515
Hello anonymous user <GoogleLoginButton callbackURL="/profile" />
@@ -19,10 +19,10 @@ export default function Profile() {
1919

2020
return (
2121
<div>
22-
<div>Hello {user.data.data.name}</div>
23-
{user.data.data.image && (
22+
<div>Hello {user.data.name}</div>
23+
{user.data.image && (
2424
<img
25-
src={user.data.data.image}
25+
src={user.data.image}
2626
alt="profile"
2727
className="w-20 h-20 rounded-full"
2828
/>

packages/web/src/pages/SignIn.tsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import { useNavigate } from "react-router-dom";
21
import { GoogleLoginButton } from "@/components/auth/GoogleLoginButton";
3-
import { onAuthChange } from "@/lib/auth-client";
4-
import { useStoreEffect } from "@/lib/utils/stores";
52

63
// TODO: style it
4+
// TODO: if user is already logged in, go to /profile
75
export default function SignIn() {
8-
const navigate = useNavigate();
9-
10-
useStoreEffect(onAuthChange, (auth) => {
11-
console.log("[auth debug] ", auth);
12-
if (auth.loggedIn) {
13-
navigate("/profile");
14-
}
15-
});
166
return (
177
<div>
188
<GoogleLoginButton callbackURL="/profile" />

packages/web/src/services/user/user.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
11
import type { UserData } from "@packages/models";
2-
import type { SelectUser } from "@packages/server/types";
3-
import { useMutation, useQuery } from "@tanstack/react-query";
2+
import { useMutation } from "@tanstack/react-query";
43
import { env } from "elysia";
54
import { api } from "@/lib/api";
6-
import { onAuthChange } from "@/lib/auth-client.ts";
5+
import { auth } from "@/lib/auth-client.ts";
76
import { queryClient } from "@/lib/tanstack/client.ts";
87
import { keys } from "@/lib/tanstack/keys";
98
import { sleep } from "@/lib/utils/sleep";
109

11-
onAuthChange.subscribe(() => {
12-
queryClient.invalidateQueries({
13-
queryKey: keys.users.currentUser,
14-
});
15-
});
16-
1710
export const useCurrentUserQuery = () => {
18-
return useQuery({
19-
queryKey: keys.users.currentUser,
20-
queryFn: async (): Promise<
21-
| {
22-
ok: true;
23-
data: SelectUser;
24-
}
25-
| {
26-
ok: false;
27-
error: Error;
28-
}
29-
> => {
30-
if (env.dev) {
31-
await sleep(500);
32-
}
33-
// TODO: set up mocker
34-
const { data, error } = await api.users.me.get();
35-
if (error) {
36-
return {
37-
ok: false,
38-
error,
39-
};
40-
}
41-
return {
42-
ok: true,
43-
data,
44-
};
45-
},
46-
});
11+
const session = auth.useSession();
12+
return {
13+
...session,
14+
data: session.data?.user,
15+
};
4716
};
4817

4918
export const useUpdateUserMutation = () => {

0 commit comments

Comments
 (0)