Skip to content

Commit

Permalink
feat: add login API integration
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed May 12, 2024
1 parent 8e970cf commit 52484ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
28 changes: 27 additions & 1 deletion my-app/src/routes/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
};
47 changes: 32 additions & 15 deletions my-app/src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
<form method="POST">
<fieldset>
<label>
Ingrese su email
<input type="email" name="email" placeholder="Email" />
</label>
<label>
Ingrese su contraseña
<input type="password" name="password" placeholder="Contaseña" />
<a href="/recover">Olvidé mi contraseña</a>
</label>
</fieldset>
<script>
import { title } from '$lib';
<button type="submit">Ingresar</button>
<a href="/register">Registrarme</a>
</form>
/** @type {import('./$types').ActionData} */
export let form;
</script>

<svelte:head>
<title>{title} - Login</title>
</svelte:head>

{#if form?.success}
<!-- this message is ephemeral; it exists because the page was rendered in
response to a form submission. it will vanish if the user reloads -->
<p>Successfully logged in!</p>
{:else}
<form method="POST">
<fieldset>
<label>
Ingrese su email
<input type="email" name="email" placeholder="Email" />
</label>
<label>
Ingrese su contraseña
<input type="password" name="password" placeholder="Contaseña" />
<a href="/recover">Olvidé mi contraseña</a>
</label>
</fieldset>

<button type="submit">Ingresar</button>
<a href="/register">Registrarme</a>
</form>
{/if}

0 comments on commit 52484ac

Please sign in to comment.