diff --git a/my-app/src/routes/login/+page.server.ts b/my-app/src/routes/login/+page.server.ts index 5a4587e..47c894e 100644 --- a/my-app/src/routes/login/+page.server.ts +++ b/my-app/src/routes/login/+page.server.ts @@ -1,5 +1,31 @@ +import { error, fail } from '@sveltejs/kit'; import type { Actions } from './$types'; +import { post } from '$lib/api'; export const actions: Actions = { - default: async (event) => {} + default: async ({ cookies, request }) => { + const data = await request.formData(); + const email = data.get('email'); + + if (!email) { + throw error(400, 'Email is required'); + } + + const password = data.get('password'); + + const body = await post('user/login', { + email: email, + password: password + }); + + if (body.errors) { + throw fail(400, body); + } + + // save JWT in cookie + const value = body.id; + cookies.set('jwt', value, { path: '/' }); + + return { success: true }; + } }; diff --git a/my-app/src/routes/login/+page.svelte b/my-app/src/routes/login/+page.svelte index af04c6c..1a30d55 100644 --- a/my-app/src/routes/login/+page.svelte +++ b/my-app/src/routes/login/+page.svelte @@ -1,16 +1,33 @@ -
+{/if}