diff --git a/web/src/routes/login/+page.server.ts b/web/src/routes/login/+page.server.ts index 25c7f4e..d9bb363 100644 --- a/web/src/routes/login/+page.server.ts +++ b/web/src/routes/login/+page.server.ts @@ -1,6 +1,7 @@ -import { type Actions, fail } from '@sveltejs/kit'; +import { fail } from '@sveltejs/kit'; +import type { Actions } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; -import { superValidate } from 'sveltekit-superforms/server'; +import { setError, superValidate } from 'sveltekit-superforms/server'; import { formSchema } from './schema'; import { HASHED_PASSWORD } from '$env/static/private'; import { generateToken } from '$lib/auth'; @@ -12,19 +13,22 @@ export const load: PageServerLoad = () => { }; export const actions: Actions = { - default: async ({ request, cookies }) => { - const data = await request.formData(); - const password = data.get('password') ?? ''; + default: async (event) => { + const form = await superValidate(event, formSchema); + + if (!form.valid) { + return fail(400, { ok: false, message: 'Bad Request', form }); + } + + const { password } = form.data; if (HASHED_PASSWORD.toLowerCase() !== Buffer.from(password.toString()).toString('hex').toLowerCase()) { - console.warn('auth failed'); - return fail(401, { message: 'Unauthorized' }); + return setError(form, 'password', 'Incorrect password.'); } const token = await generateToken(); - cookies.set('authorization', token); + event.cookies.set('authorization', token); - console.info('logged in.'); - return { message: 'Success!' }; + return { ok: true }; }, }; diff --git a/web/src/routes/login/+page.svelte b/web/src/routes/login/+page.svelte index df2028b..a1cda53 100644 --- a/web/src/routes/login/+page.svelte +++ b/web/src/routes/login/+page.svelte @@ -1,30 +1,44 @@ - - - + + +
{ + if (result.type === 'success') { + goto('/'); + } + }, + }} + >
- - - Password - - - - + + + Password + + + + - Sign In - - - + Sign In + +
+
diff --git a/web/src/routes/login/schema.ts b/web/src/routes/login/schema.ts index 2467b77..7a1b13b 100644 --- a/web/src/routes/login/schema.ts +++ b/web/src/routes/login/schema.ts @@ -1,6 +1,6 @@ import { z } from 'zod'; export const formSchema = z.object({ - password: z.string(), + password: z.string().min(1, { message: 'Password is required' }), }); export type FormSchema = typeof formSchema;