diff --git a/src/routes/reset-password/+page.server.ts b/src/routes/reset-password/+page.server.ts index 35590fcc..8a0c8776 100644 --- a/src/routes/reset-password/+page.server.ts +++ b/src/routes/reset-password/+page.server.ts @@ -3,6 +3,22 @@ import { redirect } from "@sveltejs/kit"; export const load = async ({ locals }) => { const { pb } = locals; if (pb.authStore.isValid) { - redirect(303, "/"); + throw redirect(303, "/"); } -} \ No newline at end of file +}; + +export const actions = { + reset: async ({ request, locals }) => { + const { pb } = locals; + const formData = await request.formData(); + const email = formData.get("email"); + + if (!email) { + return { error: "Email is required" }; + } + + await pb.collection("users").requestPasswordReset(email as string); + + throw redirect(303, "/login"); + } +}; diff --git a/src/routes/reset-password/+page.svelte b/src/routes/reset-password/+page.svelte index ff377596..5798b72d 100644 --- a/src/routes/reset-password/+page.svelte +++ b/src/routes/reset-password/+page.svelte @@ -1,29 +1,51 @@
-
- +
+
+ {#if !loading && !isClicked} + + {:else} + + {/if} +
diff --git a/src/routes/update-password/+page.server.ts b/src/routes/update-password/+page.server.ts index 0fcbddf1..bfed0e7f 100644 --- a/src/routes/update-password/+page.server.ts +++ b/src/routes/update-password/+page.server.ts @@ -1,8 +1,51 @@ import { redirect, error } from "@sveltejs/kit"; +import { updatePasswordSchema } from "$lib/schemas"; +import { fail } from "@sveltejs/kit"; +import { z } from "zod"; export const load = async ({ locals }) => { - const { pb, user } = locals; + const { pb } = locals; if (!pb.authStore.isValid) { redirect(303, "/login"); } -} \ No newline at end of file +} + + +export const actions = { + updatePassword: async ({ request, locals }) => { + const { pb, user } = locals; + + const formData = await request.formData(); + const postData: Record = {}; + // Build a plain object from the form data. + for (const [key, value] of formData.entries()) { + if (typeof value === "string") { + postData[key] = value; + } + } + + try { + // Validate form data using your Zod schema. + const cleanedData = updatePasswordSchema.parse(postData); + + await pb.collection("users").update(user?.id, cleanedData); + return { success: true }; + } catch (error) { + console.log(error) + if (error instanceof z.ZodError) { + // Map Zod errors to individual error messages. + const errors = { + errorOldPassword: + error.errors?.find((err) => err.path[0] === "oldPassword")?.message || "You're password is wrong", + errorPassword: + error.errors?.find((err) => err.path[0] === "password")?.message || "", + errorPasswordConfirm: + error.errors?.find((err) => err.path[0] === "passwordConfirm")?.message || "", + }; + return fail(400, { errors, zodErrors: error.errors }); + } + console.error("Unexpected error during password update:", error); + return fail(500, { error: "An unexpected error occurred" }); + } + }, +}; diff --git a/src/routes/update-password/+page.svelte b/src/routes/update-password/+page.svelte index 98cf24e2..f8076a2d 100644 --- a/src/routes/update-password/+page.svelte +++ b/src/routes/update-password/+page.svelte @@ -1,74 +1,36 @@ - + +
@@ -77,43 +39,49 @@ >

- Set a new password + Reset Your Password

+ {loading ? "Updating..." : "Update Password"} +