Password reset
Asking for a reset link and choosing a new password, each with its result.
A password reset is two screens: one that asks for a link, and one, reached from that link, that takes the new password. Each is a card with one job and a result that takes its place. Like sign in, the card is a sheet whose fields are laid out by form.
Ask for a link
<div class="sheet container-inline w-full max-w(380) p-xl" data-example="password-reset-request"> <div data-reset-ask> <form class="form"> <header class="flex flex-col gap-xs"> <h1 class="heading heading-lg">Reset your password</h1> <p class="m(0) fg-text-muted">Enter the email you sign in with. We send a link that works for 30 minutes.</p> </header> <div class="field"> <label for="reset-email">Email</label> <input class="input" id="reset-email" type="email" name="email" autocomplete="username" placeholder="you@example.com" required data-error="reset-email-error"> <span class="field-error" id="reset-email-error">Enter an email address, such as you@example.com.</span> </div> <noscript> <p class="m(0) fg-text-muted">This form needs JavaScript to send. Write to <a class="link" href="mailto:support@atlas.dev">support@atlas.dev</a>instead.</p> </noscript> <button class="btn btn-primary" disabled>Send reset link</button> <p class="m(0) text-center fg-text-muted">Remembered it? <a class="link" href="#">Back to sign in</a> </p> </form> </div> <div data-reset-result hidden> <div class="flex flex-col gap-lg"> <header class="flex flex-col items-center gap-xs text-center"> <span class="icon-frame" aria-hidden="true"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="2.5" y="5" width="15" height="10" rx="1.25" /> <path d="M3 6.5L9.8918 10.9305A0.2 0.2 0 0 0 10.1082 10.9305L17 6.5" /> </svg> </span> <h1 class="heading heading-lg mt-xs" tabindex="-1" data-reset-result-title>Check your email</h1> <p class="m(0) fg-text-muted">If an Atlas account uses <span class="fg-text-strong" data-reset-email></span>, a reset link is on its way. It works for 30 minutes.</p> </header> <div class="alert"> <div class="alert-body"> <p class="alert-description">Nothing after a few minutes? Look in the spam folder, then ask for the link again.</p> </div> </div> <button type="button" class="btn" data-reset-again>Use another email</button> <p class="m(0) text-center fg-text-muted"> <a class="link" href="#">Back to sign in</a> </p> </div> </div></div>/** * Asking for a reset link, with no mail to send. The browser checks the address: `required` and `type="email"` * are the rules. This module turns the button on, names the error from its field while it shows, and on a valid * submit swaps the form for the result, which reads the same whether or not an account has that address. * Nothing leaves the page: sending the link is the application's. * Returns the function that stops it and puts the form back. */export function mount(root) { const controller = new AbortController() const { signal } = controller const ask = root.querySelector('[data-reset-ask]') const form = ask.querySelector('.form') const email = form.elements.email const send = form.querySelector('.btn') const result = root.querySelector('[data-reset-result]') const title = result.querySelector('[data-reset-result-title]') /** The error is read with the field only while it shows: its id joins aria-describedby, and leaves with it. */ const describe = (invalid) => { if (invalid) email.setAttribute('aria-describedby', email.dataset.error) else email.removeAttribute('aria-describedby') } email.addEventListener('invalid', () => describe(true), { signal }) for (const type of ['input', 'change', 'blur']) { email.addEventListener(type, () => describe(!email.validity.valid && email.matches(':user-invalid')), { signal }) } const show = (sent) => { ask.hidden = sent result.hidden = !sent } // A submit event means the address passed; an invalid form never gets here. form.addEventListener('submit', (event) => { event.preventDefault() // The address is set as text, never as markup. result.querySelector('[data-reset-email]').textContent = email.value.trim() show(true) title.focus() }, { signal }) result.querySelector('[data-reset-again]').addEventListener('click', () => { show(false) email.focus() email.select() }, { signal }) send.disabled = false return () => { controller.abort() send.disabled = true show(false) }}Blocks are markup to copy. They take the page's own text color and size; the previews set fg-text-body text-sm on the body. Each block is a size container, so it reflows by its own width: in a wide page, a sidebar's worth of room or a dialog. Center the card on its page with the centered layout.
#What is in it
sheet w-full max-w(380) p-xl. The card fills a phone and stops at 380px, the width of the sign-in card it is reached from.- One field. The address, with
autocomplete="username", so the browser offers the one the reader signs in with. - How long the link lasts, before it is asked for. "30 minutes" is in the sentence under the title and again in the result.
- A result that tells nothing about accounts. "If an Atlas account uses this address" is true for every address. A screen that says "no such account" tells a stranger who has one.
- The address is repeated in ink in the result, so a typing mistake is seen while it can still be fixed, and "Use another email" brings the form back with the address selected.
- What to do when nothing arrives is a neutral alert: the spam folder first, then the link again.
- A way back to sign in in both states, as the last line.
#A new password
The link opens the second card. It asks for the new password twice, since the field hides what is typed, and says whose account it is. The rule, at least 12 characters, is the same one the sign-up card gives, as a field-hint shown before the reader makes the mistake.
A new password
<div class="sheet container-inline w-full max-w(380) p-xl" data-example="password-reset-new"> <div data-reset-ask> <form class="form"> <header class="flex flex-col gap-xs"> <h1 class="heading heading-lg">Choose a new password</h1> <p class="m(0) fg-text-muted">For <span class="fg-text-strong">yiching@example.com</span>. Your other devices are signed out when it changes.</p> </header> <input class="sr-only" type="email" name="email" autocomplete="username" value="yiching@example.com" readonly tabindex="-1" aria-hidden="true"> <div class="field"> <label for="reset-password">New password</label> <input class="input" id="reset-password" type="password" name="password" autocomplete="new-password" minlength="12" required aria-describedby="reset-password-hint" data-error="reset-password-error"> <span class="field-hint" id="reset-password-hint">At least 12 characters. A sentence works well.</span> <span class="field-error" id="reset-password-error">Use at least 12 characters.</span> </div> <div class="field"> <label for="reset-confirm">Confirm password</label> <input class="input" id="reset-confirm" type="password" name="confirm" autocomplete="new-password" required data-error="reset-confirm-error"> <span class="field-error" id="reset-confirm-error">Enter the same password again.</span> </div> <noscript> <p class="m(0) fg-text-muted">This form needs JavaScript to send. Write to <a class="link" href="mailto:support@atlas.dev">support@atlas.dev</a>instead.</p> </noscript> <button class="btn btn-primary" disabled>Update password</button> </form> </div> <div data-reset-result hidden> <div class="flex flex-col gap-lg"> <header class="flex flex-col items-center gap-xs text-center"> <span class="icon-frame icon-frame-success" aria-hidden="true"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3.5 10L7.8545 14.3545A0.2 0.2 0 0 0 8.1413 14.3504L16.5 5.5" /> </svg> </span> <h1 class="heading heading-lg mt-xs" tabindex="-1" data-reset-result-title>Password updated</h1> <p class="m(0) fg-text-muted">Your other devices are signed out. Sign in with the new password to go on.</p> </header> <a class="btn btn-primary" href="#">Continue to sign in</a> </div> </div></div>/** * Choosing a new password, with no account to change. The browser checks each field: `required` and * `minlength="12"` are the rules, and the second field is held to the first with a custom validity, so one * mechanism reports all three. This module turns the button on, names each error from its field while it shows, * and on a valid submit clears both passwords and shows the result. The passwords are never logged, stored or * put in a URL. Changing the account is the application's. * Returns the function that stops it, clears the fields and puts the form back. */export function mount(root) { const controller = new AbortController() const { signal } = controller const ask = root.querySelector('[data-reset-ask]') const form = ask.querySelector('.form') const { password, confirm } = form.elements const send = form.querySelector('.btn') const result = root.querySelector('[data-reset-result]') const title = result.querySelector('[data-reset-result-title]') const fields = [password, confirm] /** * A field's error is read with the field only while it shows: its id joins aria-describedby, after the hint, * when the browser reports the field, and leaves when the value is valid again. */ const describe = (control, invalid) => { const ids = (control.getAttribute('aria-describedby') ?? '').split(' ').filter(id => id && id !== control.dataset.error) if (invalid) ids.push(control.dataset.error) if (ids.length) control.setAttribute('aria-describedby', ids.join(' ')) else control.removeAttribute('aria-describedby') } const reported = (control) => !control.validity.valid && control.matches(':user-invalid') /** What the browser's own bubble says is what the line under the field says: one error has one wording. */ const mismatch = root.querySelector(`#${confirm.dataset.error}`).textContent.trim() /** The second field is valid only while it repeats the first. An empty one is left to `required`. */ const compare = () => { confirm.setCustomValidity(confirm.value && confirm.value !== password.value ? mismatch : '') describe(confirm, reported(confirm)) } for (const control of fields) { control.addEventListener('invalid', () => describe(control, true), { signal }) // Either field can end the mismatch, so both compare; an error that no longer holds leaves at once. control.addEventListener('input', compare, { signal }) for (const type of ['input', 'change', 'blur']) control.addEventListener(type, () => describe(control, reported(control)), { signal }) } const show = (done) => { ask.hidden = done result.hidden = !done } /** Empties both passwords and everything that was said about them. */ const clear = () => { form.reset() confirm.setCustomValidity('') for (const control of fields) describe(control, false) } // A submit event means every constraint passed, the match among them; an invalid form never gets here. form.addEventListener('submit', (event) => { event.preventDefault() clear() show(true) title.focus() }, { signal }) send.disabled = false return () => { controller.abort() send.disabled = true clear() show(false) }}- Both fields are
autocomplete="new-password", which is what lets a password manager offer one and save it. A read-only address field withautocomplete="username", kept out of sight and out of the tab order, tells the manager which account the password belongs to. - The match is a constraint like the others. The second field takes a custom validity while it differs from the first, so the browser reports a mismatch exactly as it reports a missing value, and the same
field-errorshows. - The result says what else changed: the other devices are signed out. Then one button, the way to sign in.
#What needs a script
The rules are the browser's: required, type="email" and minlength="12". The stylesheet draws a field as invalid from :user-invalid, which matches only after the reader has edited the field or tried to submit, so nothing is red when the page loads and an error leaves as soon as the value is valid.
Each card has a module of its own, and each does the same few things. It turns the button on: the button is disabled in the markup, so a form with no server behind it cannot be sent to the page it stands on. It adds a field's error to aria-describedby while the error shows and takes it out afterwards. On a valid submit it shows the result and moves focus to its title.
The second module also compares the two passwords on every input of either field, with setCustomValidity, so correcting the first field clears an error on the second at once. When the result appears it resets the form: both passwords are gone from the page, and they are never written to a URL, to the console or to storage.
Neither sends anything. In an application the submit handler posts the form and shows the result when the server answers; the second card also has to handle a link that has expired, with an alert alert-danger and a way to ask for another. Without the script the fields, labels and hints are there, the button stays disabled, and a <noscript> line gives an address to write to.
#Accessibility
- The card's title is the page's
<h1>. The result has an<h1>of its own withtabindex="-1", and focus moves to it, so the change of screen is announced and the keyboard continues from the answer. - Every field has a visible label. The hint is in the field's
aria-describedbyfrom the start; the error joins it only while it shows. - A failed submit keeps the browser's own behavior: focus goes to the first field that is wrong, and the browser says why.
- Each error says what to do: "Use at least 12 characters.", "Enter the same password again."
- The button is disabled only until the script is ready, never while the form is merely incomplete.
- Add no rule about capitals or symbols. Length is what makes a password hard to guess, and a rule a reader cannot see is met by trial and error.
#Related
- SurfaceTrays, sheets and wells: the material ladder every component is built on.
- FormThe rhythm of a form: rows, sections and actions that follow their column.
- InputText fields cut into the surface, with labels, hints, errors and groups.
- ButtonA raised control that lifts on hover and sinks when pressed.
- AlertA message in the flow of the page, with an optional status.
- Icon frameA small well that holds an icon at its size beside a heading.
- HeadingA title in the ink of the page, with the line over it and the paragraph under it.
- LinkText that goes somewhere, in the ink of its sentence.