also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -0,0 +1,64 @@ | ||
| 1 | import type { createMailApi } from './mail-api' | |
| 2 | import { state } from '@stacksjs/stx' | |
| 3 | import { recoveryAddressProblem } from './recovery' | |
| 4 | ||
| 5 | /** What GET/PUT /webmail/auth/recovery return. */ | |
| 6 | export interface RecoveryState { | |
| 7 | recovery: string | null | |
| 8 | /** Who set it: the mailbox owner, or the operator (deploy configuration). */ | |
| 9 | setBy: 'user' | 'operator' | null | |
| 10 | } | |
| 11 | ||
| 12 | type Api = ReturnType<typeof createMailApi> | |
| 13 | ||
| 14 | /** | |
| 15 | * The recovery address section of account settings. Saving asks for the | |
| 16 | * mailbox password again: an open session alone must not be enough to send | |
| 17 | * the account's reset links somewhere else. | |
| 18 | */ | |
| 19 | export function useRecoverySettings(api: Api, mailbox: () => string) { | |
| 20 | const recoveryAddress = state<string | null>(null) | |
| 21 | const recoverySetBy = state<RecoveryState['setBy']>(null) | |
| 22 | const recoveryInput = state('') | |
| 23 | const recoveryPassword = state('') | |
| 24 | const recoveryError = state('') | |
| 25 | const recoveryNotice = state('') | |
| 26 | const recoveryLoading = state(false) | |
| 27 | const savingRecovery = state(false) | |
| 28 | ||
| 29 | function install(result: RecoveryState): void { | |
| 30 | recoveryAddress.set(result.recovery) | |
| 31 | recoverySetBy.set(result.setBy) | |
| 32 | recoveryInput.set(result.recovery || '') | |
| 33 | } | |
| 34 | ||
| 35 | async function loadRecovery(): Promise<void> { | |
| 36 | recoveryError.set('') | |
| 37 | recoveryNotice.set('') | |
| 38 | recoveryPassword.set('') | |
| 39 | recoveryLoading.set(true) | |
| 40 | try { install(await api<RecoveryState>('/webmail/auth/recovery')) } | |
| 41 | catch (cause) { recoveryError.set(cause instanceof Error ? cause.message : 'Could not load the recovery address.') } | |
| 42 | finally { recoveryLoading.set(false) } | |
| 43 | } | |
| 44 | ||
| 45 | async function saveRecovery(): Promise<void> { | |
| 46 | if (savingRecovery()) return | |
| 47 | recoveryError.set('') | |
| 48 | recoveryNotice.set('') | |
| 49 | const wanted = recoveryInput().trim() | |
| 50 | const problem = recoveryAddressProblem(mailbox(), wanted) | |
| 51 | if (problem) { recoveryError.set(problem); return } | |
| 52 | if (!recoveryPassword()) { recoveryError.set('Enter your current password to change the recovery address.'); return } | |
| 53 | savingRecovery.set(true) | |
| 54 | try { | |
| 55 | install(await api<RecoveryState>('/webmail/auth/recovery', { method: 'PUT', body: JSON.stringify({ currentPassword: recoveryPassword(), recovery: wanted }) })) | |
| 56 | recoveryPassword.set('') | |
| 57 | recoveryNotice.set(wanted ? 'Recovery address saved.' : 'Recovery address removed. Password reset by email is off for this mailbox.') | |
| 58 | } | |
| 59 | catch (cause) { recoveryError.set(cause instanceof Error ? cause.message : 'Could not save the recovery address.') } | |
| 60 | finally { savingRecovery.set(false) } | |
| 61 | } | |
| 62 | ||
| 63 | return { recoveryAddress, recoverySetBy, recoveryInput, recoveryPassword, recoveryError, recoveryNotice, recoveryLoading, savingRecovery, loadRecovery, saveRecovery } | |
| 64 | } | |