also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -0,0 +1,137 @@ | ||
| 1 | <!DOCTYPE html> | |
| 2 | <html lang="en"> | |
| 3 | <head> | |
| 4 | <meta charset="UTF-8"> | |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| 6 | <meta name="referrer" content="no-referrer"> | |
| 7 | <title>Reset password — Mail</title> | |
| 8 | <link rel="stylesheet" href="/styles.css"> | |
| 9 | </head> | |
| 10 | <body> | |
| 11 | <script client> | |
| 12 | import { newPasswordProblem, resetErrorMessage, tokenFromHash } from '../functions/recovery' | |
| 13 | // request: ask for a link · sent: asked · checking: reading a link | |
| 14 | // reset: choose a password · invalid: dead link · done: password changed | |
| 15 | const mode = state('request') | |
| 16 | const address = state('') | |
| 17 | const account = state('') | |
| 18 | const token = state('') | |
| 19 | const password = state('') | |
| 20 | const confirmation = state('') | |
| 21 | const busy = state(false) | |
| 22 | const error = state('') | |
| 23 | ||
| 24 | async function post(path, payload) { | |
| 25 | const response = await fetch(path, { | |
| 26 | method: 'POST', credentials: 'same-origin', cache: 'no-store', | |
| 27 | headers: { 'Content-Type': 'application/json' }, | |
| 28 | body: JSON.stringify(payload), | |
| 29 | }) | |
| 30 | const result = await response.json().catch(() => ({})) | |
| 31 | return { ok: response.ok, status: response.status, result } | |
| 32 | } | |
| 33 | ||
| 34 | async function requestLink() { | |
| 35 | if (busy()) return | |
| 36 | error.set('') | |
| 37 | if (!address().trim()) { error.set('Enter your mailbox address.'); return } | |
| 38 | busy.set(true) | |
| 39 | try { | |
| 40 | const { ok, status } = await post('/webmail/auth/forgot', { address: address().trim() }) | |
| 41 | if (ok) { mode.set('sent'); return } | |
| 42 | error.set(status === 429 ? 'Too many requests. Please wait a few minutes.' : status === 400 ? 'Enter your mailbox address.' : 'Something went wrong. Please try again.') | |
| 43 | } | |
| 44 | catch { error.set('Could not reach the server. Please try again.') } | |
| 45 | finally { busy.set(false) } | |
| 46 | } | |
| 47 | ||
| 48 | async function checkLink() { | |
| 49 | const found = tokenFromHash(useRoute().hash) | |
| 50 | if (!found) return | |
| 51 | token.set(found) | |
| 52 | mode.set('checking') | |
| 53 | try { | |
| 54 | const { ok, status, result } = await post('/webmail/auth/reset/verify', { token: found }) | |
| 55 | if (ok) { account.set(result.account || ''); mode.set('reset'); return } | |
| 56 | error.set(resetErrorMessage(status)) | |
| 57 | mode.set('invalid') | |
| 58 | } | |
| 59 | catch { error.set('Could not reach the server. Reload this page to try again.'); mode.set('invalid') } | |
| 60 | } | |
| 61 | ||
| 62 | async function choosePassword() { | |
| 63 | if (busy()) return | |
| 64 | error.set('') | |
| 65 | const problem = newPasswordProblem(password(), confirmation()) | |
| 66 | if (problem) { error.set(problem); return } | |
| 67 | busy.set(true) | |
| 68 | try { | |
| 69 | const { ok, status, result } = await post('/webmail/auth/reset', { token: token(), password: password() }) | |
| 70 | if (ok) { password.set(''); confirmation.set(''); account.set(result.account || account()); mode.set('done'); return } | |
| 71 | error.set(resetErrorMessage(status, result.message || 'The password could not be changed. Please try again.')) | |
| 72 | if (status === 400 && result.code === 'invalid_token') mode.set('invalid') | |
| 73 | } | |
| 74 | catch { error.set('Could not reach the server. Please try again.') } | |
| 75 | finally { busy.set(false) } | |
| 76 | } | |
| 77 | ||
| 78 | function startOver() { error.set(''); token.set(''); mode.set('request') } | |
| 79 | ||
| 80 | onMount(checkLink) | |
| 81 | </script> | |
| 82 | <main class="login"> | |
| 83 | <div class="mb-4 font-semibold text-indigo-600 text-sm tracking-wide">MAIL</div> | |
| 84 | ||
| 85 | <section :if="mode === 'request'"> | |
| 86 | <h1>Forgot your password?</h1> | |
| 87 | <p class="login-sub">Enter your mailbox address. If it has a recovery address, we will email a reset link there.</p> | |
| 88 | <form @submit.prevent="requestLink()"> | |
| 89 | <label for="address">Mailbox address</label> | |
| 90 | <input id="address" type="text" x-model.trim="address" autocomplete="username" placeholder="you@example.com" required autofocus> | |
| 91 | <p :if="error" :text="error" class="login-error" role="alert"></p> | |
| 92 | <button type="submit" :disabled="busy" :text="busy ? 'Sending…' : 'Email me a reset link'"></button> | |
| 93 | </form> | |
| 94 | </section> | |
| 95 | ||
| 96 | <section :if="mode === 'sent'"> | |
| 97 | <h1>Check your recovery inbox</h1> | |
| 98 | <p class="login-sub" role="status">If <strong :text="address"></strong> has a recovery address, a reset link is on its way there. It works once and expires in 30 minutes.</p> | |
| 99 | <p class="login-sub">Nothing after a few minutes? Check spam, or ask your mail administrator: the mailbox may have no recovery address.</p> | |
| 100 | </section> | |
| 101 | ||
| 102 | <section :if="mode === 'checking'"> | |
| 103 | <h1>Reset your password</h1> | |
| 104 | <p class="login-sub" role="status">Checking your reset link…</p> | |
| 105 | </section> | |
| 106 | ||
| 107 | <section :if="mode === 'reset'"> | |
| 108 | <h1>Choose a new password</h1> | |
| 109 | <p class="login-sub">For <strong :text="account"></strong>. It replaces the password in webmail and in your mail apps, and signs out every session.</p> | |
| 110 | <form @submit.prevent="choosePassword()"> | |
| 111 | <input type="text" :value="account" autocomplete="username" hidden readonly> | |
| 112 | <label for="newPassword">New password</label> | |
| 113 | <input id="newPassword" type="password" x-model="password" autocomplete="new-password" minlength="12" required autofocus> | |
| 114 | <label for="confirmPassword">Confirm password</label> | |
| 115 | <input id="confirmPassword" type="password" x-model="confirmation" autocomplete="new-password" minlength="12" required> | |
| 116 | <p :if="error" :text="error" class="login-error" role="alert"></p> | |
| 117 | <button type="submit" :disabled="busy" :text="busy ? 'Saving…' : 'Set new password'"></button> | |
| 118 | </form> | |
| 119 | </section> | |
| 120 | ||
| 121 | <section :if="mode === 'invalid'"> | |
| 122 | <h1>This link cannot be used</h1> | |
| 123 | <p :text="error" class="login-error" role="alert"></p> | |
| 124 | <button type="button" @click="startOver()">Ask for a new link</button> | |
| 125 | </section> | |
| 126 | ||
| 127 | <section :if="mode === 'done'"> | |
| 128 | <h1>Password changed</h1> | |
| 129 | <p class="login-sub" role="status">Sign in to <strong :text="account"></strong> with your new password, and update it in your mail apps.</p> | |
| 130 | <a class="login-link" href="/login">Sign in</a> | |
| 131 | </section> | |
| 132 | ||
| 133 | <p :if="mode !== 'done'" class="login-sub"><a class="login-link" href="/login">Back to sign in</a></p> | |
| 134 | <noscript>JavaScript is required to reset your password.</noscript> | |
| 135 | </main> | |
| 136 | </body> | |
| 137 | </html> | |