From 88ebfc188c971733e013473ba5623d37b2ec93ec Mon Sep 17 00:00:00 2001 From: Emily Date: Mon, 13 Jan 2025 17:01:34 +0100 Subject: [PATCH] add password reset + password change --- dashboard/components/settings/Account.vue | 52 ++++++ dashboard/middleware/01.client_auth.global.ts | 3 +- dashboard/pages/forgot_password.vue | 150 ++++++++++++++++++ dashboard/pages/login.vue | 19 ++- .../server/api/user/password/can_change.ts | 10 ++ .../server/api/user/password/change.post.ts | 33 ++++ dashboard/server/api/user/password/reset.ts | 26 +++ shared/services/EmailService.ts | 19 +++ .../email_templates/ResetPasswordEmail.ts | 109 +++++++++++++ 9 files changed, 416 insertions(+), 5 deletions(-) create mode 100644 dashboard/pages/forgot_password.vue create mode 100644 dashboard/server/api/user/password/can_change.ts create mode 100644 dashboard/server/api/user/password/change.post.ts create mode 100644 dashboard/server/api/user/password/reset.ts create mode 100644 shared/services/email_templates/ResetPasswordEmail.ts diff --git a/dashboard/components/settings/Account.vue b/dashboard/components/settings/Account.vue index 0fd411c..05c1bef 100644 --- a/dashboard/components/settings/Account.vue +++ b/dashboard/components/settings/Account.vue @@ -2,12 +2,18 @@ import type { SettingsTemplateEntry } from './Template.vue'; const entries: SettingsTemplateEntry[] = [ + { id: 'change_pass', title: 'Change password', text: 'Change your password' }, { id: 'delete', title: 'Delete account', text: 'Delete your account' }, ] +const { user } = useLoggedUser(); const { setToken } = useAccessToken(); +const canChangePassword = useFetch('/api/user/password/can_change', { + headers: useComputedHeaders({ useSnapshotDates: false }) +}); + async function deleteAccount() { const sure = confirm("Are you sure you want to delete this account ?"); if (!sure) return; @@ -20,11 +26,57 @@ async function deleteAccount() { location.href = "/login" } +const old_password = ref(""); +const new_password = ref(""); + +const { createAlert } = useAlert() + +async function changePassword() { + + + try { + const res = await $fetch("/api/user/password/change", { + ...signHeaders({ 'Content-Type': 'application/json' }), + method: "POST", + body: JSON.stringify({ old_password: old_password.value, new_password: new_password.value }) + }) + + if (!res) throw Error('No response'); + + if (res.error) return createAlert('Error', res.message, 'far fa-triangle-exclamation', 5000); + + + old_password.value = ''; + new_password.value = ''; + + return createAlert('Success', 'Password changed successfully', 'far fa-circle-check', 5000); + + } catch (ex) { + console.error(ex); + createAlert('Error', 'Internal error', 'far fa-triangle-exclamation', 5000); + } + +} + + +