From 54c7460fc46ef8187685a8025017b4c95ef376ed Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Wed, 20 Sep 2023 12:46:32 +1200 Subject: [PATCH] Implementing logged in password change. --- src/lib/config/zod-schemas.ts | 28 +++++++ .../(app)/(protected)/list/+layout.svelte | 2 +- .../password/change/+page.server.ts | 74 +++++++++++++++++++ .../(protected)/password/change/+page.svelte | 55 ++++++++++++++ .../password/reset/+page.server.ts | 0 .../(protected)}/password/reset/+page.svelte | 0 .../(app)/(protected)/profile/+page.svelte | 8 +- src/routes/(auth)/sign-up/+page.svelte | 9 ++- 8 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 src/routes/(app)/(protected)/password/change/+page.server.ts create mode 100644 src/routes/(app)/(protected)/password/change/+page.svelte rename src/routes/{(auth) => (app)/(protected)}/password/reset/+page.server.ts (100%) rename src/routes/{(auth) => (app)/(protected)}/password/reset/+page.svelte (100%) diff --git a/src/lib/config/zod-schemas.ts b/src/lib/config/zod-schemas.ts index 301b518..3988d4f 100644 --- a/src/lib/config/zod-schemas.ts +++ b/src/lib/config/zod-schemas.ts @@ -90,3 +90,31 @@ export const updateUserPasswordSchema = userSchema }); } }); + +export const changeUserPasswordSchema = z + .object({ + current_password: z.string({ required_error: 'Current Password is required' }), + password: z + .string({ required_error: 'Password is required' }) + .trim() + .min(8, { message: 'Password must be at least 8 characters' }) + .max(128, { message: 'Password must be less than 128 characters' }), + confirm_password: z + .string({ required_error: 'Confirm Password is required' }) + .trim() + .min(8, { message: 'Confirm Password must be at least 8 characters' }) + }) + .superRefine(({ confirm_password, password }, ctx) => { + if (confirm_password !== password) { + ctx.addIssue({ + code: 'custom', + message: 'Password and Confirm Password must match', + path: ['password'] + }); + ctx.addIssue({ + code: 'custom', + message: 'Password and Confirm Password must match', + path: ['confirm_password'] + }); + } + }); diff --git a/src/routes/(app)/(protected)/list/+layout.svelte b/src/routes/(app)/(protected)/list/+layout.svelte index 071ea58..cbd6fc5 100644 --- a/src/routes/(app)/(protected)/list/+layout.svelte +++ b/src/routes/(app)/(protected)/list/+layout.svelte @@ -1,5 +1,5 @@ + +
+ +

Change Password

+
+ {#if $message} + + {/if} +
+ + + {#if $errors.current_password} + {$errors.current_password} + {/if} +
+
+ + + {#if $errors.password} + {$errors.password} + {/if} +
+
+ + + {#if $errors.confirm_password} + {$errors.confirm_password} + {/if} +
+ +
+ +
+
\ No newline at end of file diff --git a/src/routes/(auth)/password/reset/+page.server.ts b/src/routes/(app)/(protected)/password/reset/+page.server.ts similarity index 100% rename from src/routes/(auth)/password/reset/+page.server.ts rename to src/routes/(app)/(protected)/password/reset/+page.server.ts diff --git a/src/routes/(auth)/password/reset/+page.svelte b/src/routes/(app)/(protected)/password/reset/+page.svelte similarity index 100% rename from src/routes/(auth)/password/reset/+page.svelte rename to src/routes/(app)/(protected)/password/reset/+page.svelte diff --git a/src/routes/(app)/(protected)/profile/+page.svelte b/src/routes/(app)/(protected)/profile/+page.svelte index 4696a1e..6db738a 100644 --- a/src/routes/(app)/(protected)/profile/+page.svelte +++ b/src/routes/(app)/(protected)/profile/+page.svelte @@ -2,7 +2,7 @@ import { superForm } from 'sveltekit-superforms/client'; //import SuperDebug from 'sveltekit-superforms/client/SuperDebug.svelte'; import { userSchema } from '$lib/config/zod-schemas'; - import { AlertTriangle } from 'lucide-svelte'; + import { AlertTriangle, KeyRound } from 'lucide-svelte'; import { Label } from '$components/ui/label'; import { Input } from '$components/ui/input'; import { Button } from '$components/ui/button'; @@ -63,8 +63,10 @@ {/if}
- - +
diff --git a/src/routes/(auth)/sign-up/+page.svelte b/src/routes/(auth)/sign-up/+page.svelte index 9f79a1d..52683d6 100644 --- a/src/routes/(auth)/sign-up/+page.svelte +++ b/src/routes/(auth)/sign-up/+page.svelte @@ -1,5 +1,7 @@