From 3b1e6273560601139173d8ab261648316ee31ce4 Mon Sep 17 00:00:00 2001 From: Omer Sabic Date: Tue, 12 Mar 2024 20:14:29 +0100 Subject: [PATCH] lots of changes --- src/app.d.ts | 17 ++++- src/hooks.server.js | 3 +- .../organisms/auth/login-form.svelte | 69 +++++++++++++++++++ src/lib/components/organisms/auth/schema.ts | 11 ++- .../{auth-form.svelte => signup-form.svelte} | 8 +-- src/lib/utils/auth.utils.ts | 10 +++ src/routes/+page.svelte | 31 +++++---- src/routes/api/pods/+server.js | 40 ++++------- src/routes/auth/+page.server.js | 49 +++++++++++-- src/routes/auth/+page.svelte | 17 ++++- 10 files changed, 197 insertions(+), 58 deletions(-) create mode 100644 src/lib/components/organisms/auth/login-form.svelte rename src/lib/components/organisms/auth/{auth-form.svelte => signup-form.svelte} (92%) diff --git a/src/app.d.ts b/src/app.d.ts index 743f07b..bf2938e 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,11 +3,26 @@ declare global { namespace App { // interface Error {} - // interface Locals {} + interface Locals { + user: SessionUserInfo, + project: SessionProjectInfo + } // interface PageData {} // interface PageState {} // interface Platform {} } + + interface SessionUserInfo { + id: string, + username: string, + email: string, + account_type: string + } + + interface SessionProjectInfo { + id: string, + project_name: string + } } export {}; diff --git a/src/hooks.server.js b/src/hooks.server.js index 81f1a07..8da02f7 100644 --- a/src/hooks.server.js +++ b/src/hooks.server.js @@ -2,8 +2,9 @@ import { validateSession } from '$lib/services/auth.server'; /** @type {import('@sveltejs/kit').Handle} */ export async function handle({ event, resolve }) { + if(event.url.pathname === "/auth") return resolve(event); const session_id = event.cookies.get("token"); - if (session_id === undefined && event.url.pathname !== "/auth") return Response.redirect(event.url.host+"/auth", 303); + if (session_id === undefined) return Response.redirect(event.url.host+"/auth", 303); // @ts-ignore const user = await validateSession(session_id); diff --git a/src/lib/components/organisms/auth/login-form.svelte b/src/lib/components/organisms/auth/login-form.svelte new file mode 100644 index 0000000..5b53154 --- /dev/null +++ b/src/lib/components/organisms/auth/login-form.svelte @@ -0,0 +1,69 @@ + + + +
+ {#if $message} + {$message} + {/if} + + + Email + + + + + + + + Password + + + + A strong password with 6-20 characters. + + + {#if isLoading} + + {:else} + Submit + {/if} + +
+ \ No newline at end of file diff --git a/src/lib/components/organisms/auth/schema.ts b/src/lib/components/organisms/auth/schema.ts index 2121f50..c0a8e8d 100644 --- a/src/lib/components/organisms/auth/schema.ts +++ b/src/lib/components/organisms/auth/schema.ts @@ -1,9 +1,16 @@ import { z } from "zod"; -export const formSchema = z.object({ +export const signupSchema = z.object({ name: z.string().min(2).max(16), email: z.string().min(2).max(50).email("Invalid email format"), password: z.string().min(6).max(20), }); -export type FormSchema = typeof formSchema; \ No newline at end of file +export type SignupSchema = typeof signupSchema; + +export const loginSchema = z.object({ + email: z.string().min(2).max(50).email("Invalid email format"), + password: z.string().min(6).max(20), +}); + +export type LoginSchema = typeof loginSchema; \ No newline at end of file diff --git a/src/lib/components/organisms/auth/auth-form.svelte b/src/lib/components/organisms/auth/signup-form.svelte similarity index 92% rename from src/lib/components/organisms/auth/auth-form.svelte rename to src/lib/components/organisms/auth/signup-form.svelte index 26926a3..e70fed0 100644 --- a/src/lib/components/organisms/auth/auth-form.svelte +++ b/src/lib/components/organisms/auth/signup-form.svelte @@ -2,7 +2,7 @@ import * as Form from "$lib/components/ui/form"; import { Input } from "$lib/components/ui/input"; import { Loader2 } from "lucide-svelte"; - import { formSchema, type FormSchema } from "./schema"; + import { signupSchema, type SignupSchema } from "./schema"; import { type SuperValidated, type Infer, @@ -12,10 +12,10 @@ let isLoading = false; - export let data: SuperValidated>; + export let data: SuperValidated>; const form = superForm(data, { - validators: zodClient(formSchema), + validators: zodClient(signupSchema), onSubmit: () => { isLoading = true; }, @@ -66,8 +66,8 @@ disabled={isLoading} /> - A strong password with 6-20 characters. + A strong password with 6-20 characters. {#if isLoading} diff --git a/src/lib/utils/auth.utils.ts b/src/lib/utils/auth.utils.ts index 492f029..79b7cc4 100644 --- a/src/lib/utils/auth.utils.ts +++ b/src/lib/utils/auth.utils.ts @@ -1,7 +1,17 @@ +import bcrypt from "bcrypt"; + export function generateId(length: number): string { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; +} + +export async function hashPassword(password: string): Promise { + return await bcrypt.hash(password, 10); +} + +export async function validatePassword(hashed_password: string, plain_password: string) { + return await bcrypt.compare(plain_password, hashed_password); } \ No newline at end of file diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 69afb38..f2c2eb5 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -4,7 +4,7 @@ import { Button } from '$lib/components/ui/button'; import Player from '$lib/components/organisms/player.svelte'; import { onMount } from 'svelte'; - import { PlayIcon, Loader } from 'lucide-svelte'; + import { PlayIcon, Loader, Plus, MailIcon } from 'lucide-svelte'; let script = ""; @@ -27,23 +27,26 @@ opened=false} /> {/if} -
- - +
+ + {#each pods as pod} + + {/each} +
- {#each pods as pod, i} + {#each pods as pod}