Skip to content

Commit 032ad76

Browse files
feat: fix errors (#377)
* add * add * add * add * add * Update login_form.svelte * add * add * add * add * Update login_form.svelte * add * add * ad * add * Update config.ts * add * add * Update +page.svelte
1 parent 2e8c8d4 commit 032ad76

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/frontend/src/lib/queries/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { ADMIN_USER_UPDATE_URL, USER_URL } from '#consts/backend';
22
import { browser } from '$app/environment';
3+
import { user_store } from '$lib/store/user.svelte';
34
import { createQuery, useQueryClient } from '@tanstack/svelte-query';
4-
5+
const { is_authenticated } = user_store();
56
const queryKey = ['auth-user'];
67

78
const fetchUser = async ({
89
fetch = globalThis.window.fetch
910
}: {
1011
fetch?: typeof globalThis.window.fetch;
1112
}) => {
12-
if (!browser) return null;
13+
if (!is_authenticated) return null;
1314
const res = await fetch(USER_URL, {
1415
credentials: 'include'
1516
});

src/frontend/src/lib/store/user.svelte.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ let authenticated: null | boolean = null;
22

33
export const user_store = () => {
44
return {
5+
unauthenticate() {
6+
authenticated = false;
7+
},
58
authenticate() {
69
authenticated = true;
710
},

src/frontend/src/routes/(needs_onboarding)/login/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LOGIN_URL } from '#consts/backend';
22
import { fail } from '@sveltejs/kit';
3-
import { message, superValidate } from 'sveltekit-superforms';
3+
import { message, setError, superValidate } from 'sveltekit-superforms';
44
import { zod4 } from 'sveltekit-superforms/adapters';
55
import { schema } from './schema';
66

@@ -22,7 +22,7 @@ export const actions = {
2222
});
2323

2424
if (!res.ok) {
25-
throw new Error('Invalid username or password');
25+
return setError(form, '', 'Invalid username or password');
2626
}
2727

2828
const data = await res.json();

src/frontend/src/routes/(needs_onboarding)/login/login_form.svelte

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import { Button } from '$lib/components/ui/button';
1010
import { untrack } from 'svelte';
1111
import { goto } from '$app/navigation';
12-
import { useAuth } from '#queries/auth';
1312
import { toast } from 'svelte-sonner';
1413
1514
let showPassword = $state(false);
@@ -25,7 +24,13 @@
2524
if (form.valid) {
2625
goto(next_url);
2726
} else {
28-
toast.error('Please fix the errors in the form.');
27+
const globalErrors = form.errors._errors;
28+
29+
if (globalErrors && globalErrors.length > 0) {
30+
toast.error(globalErrors[0]);
31+
} else {
32+
toast.error('Please check your credentials.');
33+
}
2934
}
3035
}
3136
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
import { user_store } from '$lib/store/user.svelte';
3+
import { page } from '$app/state';
4+
import { onMount } from 'svelte';
5+
6+
const { unauthenticate } = user_store();
7+
8+
const next = $derived.by(() => {
9+
const url = page.url.searchParams.get('next') ?? '/';
10+
if (url.startsWith('/admin')) {
11+
return '/';
12+
}
13+
return url;
14+
});
15+
16+
onMount(() => {
17+
unauthenticate();
18+
window.location.href = next;
19+
});
20+
</script>

0 commit comments

Comments
 (0)