diff --git a/src/db/users.ts b/src/db/users.ts index de87e15..e493d3b 100644 --- a/src/db/users.ts +++ b/src/db/users.ts @@ -1,7 +1,7 @@ import { config } from "$lib/configuration"; import { generateRandomToken } from "$lib/randomToken"; import type { Email, Password, VerificationCode } from "$lib/types"; -import { hash } from "@node-rs/argon2"; +import { hash, verify } from "@node-rs/argon2"; import { db } from "."; import { usersTable } from "./schema"; import { eq } from "drizzle-orm"; @@ -36,6 +36,22 @@ export async function createNewUser(email: Email, password: Password): Promise<{ } } +export async function loadUser(id: number) { + const user = await db.select().from(usersTable).where(eq(usersTable.id, id)).limit(1); + if (user.length === 0) { + return null; + } + return { + id: user[0].id, + email: user[0].email as Email, + password_hash: user[0].password_hash + } +} + +export async function verifyPassword(password: Password, password_hash: string) { + return await verify(password_hash, password); +} + export enum VerificationError { InvalidVerificationCode = 'Invalid verification code', VerificationCodeExpired = 'Verification code expired' diff --git a/src/lib/components/PasswordSetterFormPart.svelte b/src/lib/components/PasswordSetterFormPart.svelte new file mode 100644 index 0000000..e81db3e --- /dev/null +++ b/src/lib/components/PasswordSetterFormPart.svelte @@ -0,0 +1,27 @@ + + + + + + diff --git a/src/lib/components/icons/ProfileIcon.svelte b/src/lib/components/icons/ProfileIcon.svelte new file mode 100644 index 0000000..5e41cb6 --- /dev/null +++ b/src/lib/components/icons/ProfileIcon.svelte @@ -0,0 +1,14 @@ + + + diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 02cf00f..108340e 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -6,9 +6,30 @@ import Navbar from '$lib/components/Navbar.svelte'; import CheckIcon from '$lib/components/icons/CheckIcon.svelte'; import DuplicateIcon from '$lib/components/icons/DuplicateIcon.svelte'; + import { page } from '$app/state'; + import { toast } from '@zerodevx/svelte-toast'; + import { goto } from '$app/navigation'; + import ProfileIcon from '$lib/components/icons/ProfileIcon.svelte'; + + $effect(() => { + if (page.url.searchParams.get('pwd_updated') === 'true') { + toast.push('Password update successful', { + theme: { + '--toastProgressBackground': 'green' + }, + onpop: () => { + goto('/'); + } + }); + } + }); - + +
+ +
+

Surveys you own

diff --git a/src/routes/(app)/passwordChange/+page.server.ts b/src/routes/(app)/passwordChange/+page.server.ts new file mode 100644 index 0000000..fc121fe --- /dev/null +++ b/src/routes/(app)/passwordChange/+page.server.ts @@ -0,0 +1,33 @@ +import type { Email, Password } from "$lib/types"; +import { error, redirect, type Actions } from "@sveltejs/kit"; +import { db } from "../../../db"; +import { usersTable } from "../../../db/schema"; +import { loadUser, verifyPassword } from "../../../db/users"; +import { hash } from "@node-rs/argon2"; +import { eq } from "drizzle-orm"; + +export const actions = { + default: async (event) => { + const formData = await event.request.formData(); + const old_password = formData.get('old_password')?.toString() as Password | undefined; + const password = formData.get('password')?.toString() as Password | undefined; + + if (!password || !old_password) { + error(400, 'Old and new password is required'); + } + + if (event.locals.userId === null) { + error(403, 'User is not logged in'); + } + // load and verify the old credentials and update the password hash + const user = await loadUser(event.locals.userId); + if (!user) { + error(403, 'User does not exist'); + } + if (await verifyPassword(old_password, user.password_hash)) { + await db.update(usersTable).set({ password_hash: await hash(password) }).where(eq(usersTable.id, event.locals.userId)); + } + + redirect(303, '/?pwd_updated=true'); + } +} satisfies Actions; \ No newline at end of file diff --git a/src/routes/(app)/passwordChange/+page.svelte b/src/routes/(app)/passwordChange/+page.svelte new file mode 100644 index 0000000..812168b --- /dev/null +++ b/src/routes/(app)/passwordChange/+page.svelte @@ -0,0 +1,36 @@ + + + + +

+ Please provide your old password and the new password twice to update your password. +

+
+ + + + + + diff --git a/src/routes/register/+page.svelte b/src/routes/register/+page.svelte index f4a33a1..62bdafa 100644 --- a/src/routes/register/+page.svelte +++ b/src/routes/register/+page.svelte @@ -1,8 +1,9 @@ @@ -19,24 +20,7 @@ {/if} - - - - +