From f9aae20f91a4858a683f9d573e531978c5982dc1 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Thu, 12 May 2022 23:19:30 -0700 Subject: [PATCH] Fix login page endpoint, implement session passing, redirect on protected routes and login. --- src/app.d.ts | 8 +- src/hooks.ts | 33 +++++++ src/lib/api.ts | 3 +- src/routes/auth/login/index.svelte | 96 ++++++++++---------- src/routes/auth/login/{login.ts => index.ts} | 2 - src/routes/auth/register/index.svelte | 94 +++++++++++-------- src/routes/protected/index.svelte | 27 ++++++ 7 files changed, 170 insertions(+), 93 deletions(-) create mode 100644 src/hooks.ts rename src/routes/auth/login/{login.ts => index.ts} (95%) create mode 100644 src/routes/protected/index.svelte diff --git a/src/app.d.ts b/src/app.d.ts index 121720c..7b24d58 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,8 +3,12 @@ // See https://kit.svelte.dev/docs/types#app // for information about these interfaces declare namespace App { - // interface Locals {} + interface Locals { + user?: { username: string } + } // interface Platform {} - // interface Session {} + interface Session { + user?: { username: string } + } // interface Stuff {} } diff --git a/src/hooks.ts b/src/hooks.ts new file mode 100644 index 0000000..c5517e0 --- /dev/null +++ b/src/hooks.ts @@ -0,0 +1,33 @@ +import type { GetSession, Handle } from "@sveltejs/kit" +import * as cookie from 'cookie' + +import { db } from '$lib/database' + +export const handle: Handle = async ({ event, resolve }) => { + const cookieHeader = event.request.headers.get('cookie') + const cookies = cookie.parse(cookieHeader ?? '') + + if (!cookies.session) { + return await resolve(event) + } + + const session = await db.user.findUnique({ + where: { id: cookies.session }, + }) + + if (session) { + event.locals.user = { username: session.username } + } + + return await resolve(event) +} + +export const getSession: GetSession = ({ locals }) => { + if (!locals.user) return {} + + return { + user: { + username: locals.user.username + } + } +} \ No newline at end of file diff --git a/src/lib/api.ts b/src/lib/api.ts index d4931c6..96db1a3 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -8,8 +8,7 @@ export async function send(form: HTMLFormElement): Send { const response = await fetch(form.action, { method: form.method, body: new FormData(form), - headers: { accept: 'application/json' } + headers: { accept: 'application/json' }, }) - return await response.json() } \ No newline at end of file diff --git a/src/routes/auth/login/index.svelte b/src/routes/auth/login/index.svelte index a3ef9d0..fa45ace 100644 --- a/src/routes/auth/login/index.svelte +++ b/src/routes/auth/login/index.svelte @@ -1,57 +1,57 @@ - -
-
- - -
+ + + +
+ + +
+ +
+ + +
+ + {#if error} +

{error}

+ {/if} + +
\ No newline at end of file + .error { + color: tomato; + } + diff --git a/src/routes/auth/login/login.ts b/src/routes/auth/login/index.ts similarity index 95% rename from src/routes/auth/login/login.ts rename to src/routes/auth/login/index.ts index f90c02b..69d58b3 100644 --- a/src/routes/auth/login/login.ts +++ b/src/routes/auth/login/index.ts @@ -7,9 +7,7 @@ import { db } from '$lib/database' export const post: RequestHandler = async ({ request }) => { const form = await request.formData() const username = form.get('username') - console.log('username', username); const password = form.get('password') - console.log('password', password); if ( typeof username !== 'string' || diff --git a/src/routes/auth/register/index.svelte b/src/routes/auth/register/index.svelte index 35b23a6..fc2283c 100644 --- a/src/routes/auth/register/index.svelte +++ b/src/routes/auth/register/index.svelte @@ -1,55 +1,71 @@ + +
-
- -
-
- -
+
+ +
+
+ +
- {#if error} -

{error}

- {/if} + {#if error} +

{error}

+ {/if} - {#if success} -
-

Thank you for signing up!

-

You can log in.

-
- {/if} + {#if success} +
+

Thank you for signing up!

+

You can log in.

+
+ {/if} - +
\ No newline at end of file + .error { + color: tomato; + } + diff --git a/src/routes/protected/index.svelte b/src/routes/protected/index.svelte new file mode 100644 index 0000000..144f832 --- /dev/null +++ b/src/routes/protected/index.svelte @@ -0,0 +1,27 @@ + + + + +

Protected

+ +

Weclome {user}