Skip to content

Commit 65ea8ed

Browse files
committed
Update login
1 parent 0c053f7 commit 65ea8ed

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

app/src/models/user.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ impl User {
3939

4040
pub async fn authorize_by_guest_id(state: &AppState) -> Result<User> {
4141
let user_id = Self::get_next_guest_id(state, 0).await?;
42-
let guest_name: String = fake::faker::name::en::Name().fake();
42+
let guest_name: String = fake::faker::name::en::FirstName().fake();
43+
let last_name: String = fake::faker::name::en::LastName().fake();
44+
let first_char_of_last_name: String = last_name.chars().next().unwrap().to_string();
45+
let guest_name = format!("{} {}.", guest_name, first_char_of_last_name);
4346

4447
let user = sqlx::query_as!(
4548
User,

web/src/api/api.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { authStore, useAuth } from "@/hooks/auth";
44

55
export const baseUrl = new URL('/api/', window.location.origin);
66

7+
let tokenProxy = {
8+
get value() {
9+
return authStore.getSnapshot().context.token;
10+
},
11+
}
12+
713
export const useApi = createFetch<paths>({
814
baseUrl,
915
get headers() {
1016
return {
11-
Authorization: `Bearer ${authStore.getSnapshot().context.token}`,
17+
Authorization: `Bearer ${tokenProxy.value}`,
1218
};
1319
},
1420
});

web/src/api/auth.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ export const useGuestAuth = (extra?: Partial<MutationOptions<GuestResponse, unde
1919
}
2020

2121
export const useUser = () => {
22-
const { token, user } = useAuth();
23-
22+
const { token } = useAuth();
23+
2424
return useQuery({
2525
queryKey: ['user', token],
2626
queryFn: async () => {
27-
console.log('fetching user w auth token', token);
28-
if (!token) {
29-
return null;
30-
}
31-
3227
try {
33-
const response = await useApi('/auth/user', 'get', {})
28+
console.log('fetching user');
29+
const response = await useApi('/auth/user', 'get', { fetchOptions: { cache: 'no-store', headers: { Authorization: `Bearer ${token}` } } })
30+
console.log('user response', response.data);
3431
return response.data;
3532
} catch (error) {
3633
console.error('Error fetching user data:', error);
@@ -40,10 +37,11 @@ export const useUser = () => {
4037
// Use the user from auth context as initialData
4138
// initialData: user,
4239
// Only run query if we have a token
43-
enabled: !!token,
40+
// enabled: !!token,
4441
// Reasonable refetch settings
4542
refetchOnMount: true,
4643
refetchOnWindowFocus: true,
4744
staleTime: 3000, // Consider data stale after 3 seconds
45+
refetchInterval: 5000,
4846
});
4947
};

web/src/hooks/auth.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ export const authStore = createStore({
5151
};
5252

5353
// Notify listeners of state change
54-
setTimeout(() => authEvents.notify(), 0);
55-
queryClient.invalidateQueries({}).then(() => {
56-
queryClient.refetchQueries({});
57-
});
54+
setTimeout(() => {
55+
authEvents.notify();
56+
57+
console.log('invalidating queries');
58+
queryClient.invalidateQueries({}).then(() => {
59+
console.log('refetching queries');
60+
queryClient.refetchQueries({});
61+
});
62+
}, 0);
5863

5964
return result;
6065
},
@@ -70,9 +75,6 @@ export const authStore = createStore({
7075

7176
// Notify listeners of state change
7277
setTimeout(() => authEvents.notify(), 0);
73-
queryClient.invalidateQueries({}).then(() => {
74-
queryClient.refetchQueries({});
75-
});
7678

7779
return result;
7880
},
@@ -86,9 +88,6 @@ export const authStore = createStore({
8688

8789
// Notify listeners of state change
8890
setTimeout(() => authEvents.notify(), 0);
89-
queryClient.invalidateQueries({}).then(() => {
90-
queryClient.refetchQueries({});
91-
});
9291

9392
return result;
9493
}
@@ -172,3 +171,16 @@ export const useAuth = () => {
172171
}
173172
};
174173
};
174+
175+
// subscribe to auth state changes and invalidate queries when user is logged in
176+
177+
// authStore.subscribe(() => {
178+
// // if (authStore.getSnapshot().context.token) {
179+
// setTimeout(() => {
180+
// queryClient.invalidateQueries({}).then(() => {
181+
// queryClient.refetchQueries({});
182+
// });
183+
// }, 1000);
184+
// // }
185+
// });
186+

web/src/routes/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const Route = createFileRoute('/')({
1212
})
1313

1414
function RouteComponent() {
15-
return <div className="mx-auto w-full max-w-4xl flex flex-wrap gap-4 pt-8">
15+
return <div className="mx-auto w-full max-w-4xl flex flex-wrap gap-4 pt-8 px-2">
1616
<div className="card flex-1 flex flex-col gap-1">
1717
<h1 className="">Let&apos;s fish some base codes!</h1>
1818
<p className="text-secondary">Code raiding is the age-old process of endlessly trying codes on your enemies base until you find one that works.</p>
@@ -34,7 +34,7 @@ function RouteComponent() {
3434
<h2 className="text-accent pt-4">Let&apos;s go!</h2>
3535
<p className="text-secondary">Use this site to coordinate with ur group. And profit efficiently.</p>
3636
</div>
37-
<div className="flex h-fit gap-4 flex-col">
37+
<div className="flex h-fit gap-4 flex-col w-full sm:max-w-sm">
3838
<div className="card flex-1 flex flex-col gap-1">
3939
<h2>Create a party</h2>
4040
<p className="text-secondary">Start a code raid</p>
@@ -43,7 +43,7 @@ function RouteComponent() {
4343
<div className="card flex-1 flex flex-col gap-1">
4444
<h2>Join a party</h2>
4545
<p className="text-secondary">Enter party code to join a party.</p>
46-
<div className="flex gap-2">
46+
<div className="flex gap-2 w-full flex-wrap">
4747
<input type="text" placeholder="Party code" className="input" />
4848
<button className="button grow h-full">Join</button>
4949
</div>

0 commit comments

Comments
 (0)