重置密码由两个页面组成:一个用来申请链接,另一个从该链接进入,用来设置新密码。每个页面都是一张只做一件事的卡片,完成后由结果取代它的位置。与登录一样,这张卡片是一块面板,其中的字段由表单负责排布。
申请链接
<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) }}区块提供可直接复制的标记,沿用页面的文字颜色和字号。预览在 body 上设置 fg-text-body text-sm。每个区块都是尺寸容器,无论放在宽页面、只有侧边栏那么宽的区域,还是对话框中,都会按自身宽度重排。使用居中布局将卡片放在页面中央。
#区块组成
sheet w-full max-w(380) p-xl。 卡片在手机上占满可用宽度,最大宽度为 380px,与通向它的登录卡片同宽。- 一个字段。 邮箱地址,设置
autocomplete="username",这样浏览器会提供用户登录时使用的那个地址。 - 在申请之前就说明链接的有效期。 “30 minutes”(30 分钟)写在标题下的那句话里,在结果中再出现一次。
- 结果不透露任何账户信息。 “If an Atlas account uses this address”(如果有 Atlas 账户使用这个地址)对每个地址都成立。显示“没有这个账户”的页面,等于告诉陌生人谁有账户。
- 结果中用墨色重复显示地址,这样输入错误在还能改正时就会被发现;“Use another email”(换一个邮箱)会让表单回来,并选中其中的地址。
- 什么都没收到时该怎么办用一个中性行内提示说明:先查看垃圾邮件文件夹,再重新申请链接。
- 返回登录的入口在两种状态下都有,位于最后一行。
#新密码
链接打开的是第二张卡片。因为字段会隐藏输入的内容,它要求输入两次新密码,并说明这是谁的账户。密码规则是至少 12 个字符,与注册卡片给出的规则相同,用 field-hint 在用户输错之前就显示出来。
新密码
<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) }}- 两个字段都设置
autocomplete="new-password",密码管理器正是凭借它才能提供并保存一个新密码。另有一个只读的地址字段,设置autocomplete="username",不显示在页面上,也不在 Tab 顺序中,用来告诉密码管理器这个密码属于哪个账户。 - 两次输入一致与其他规则一样,也是一条约束。 第二个字段与第一个不同时,会被设置自定义有效性,因此浏览器报告不一致的方式与报告缺少值完全相同,显示的也是同一个
field-error。 - 结果说明还有什么发生了变化:其他设备已退出登录。然后是一个按钮,即登录入口。
#哪些操作需要脚本
验证规则由浏览器负责:required、type="email" 和 minlength="12"。样式表根据 :user-invalid 把字段绘制为无效状态,它只在用户编辑过该字段或尝试提交之后才会匹配,因此页面加载时不会出现任何红色,值一旦有效,错误提示也随即消失。
每张卡片都有自己的模块,各自做同样的几件事。它启用按钮:按钮在标记中是 disabled 的,因此背后没有服务器的表单不会被提交到它所在的页面。错误提示显示时,它把该字段的错误信息加入 aria-describedby,提示消失后再移除。提交通过验证后,它显示结果,并把焦点移到结果的标题上。
第二个模块还会在两个字段中任意一个触发 input 时,用 setCustomValidity 比较两个密码,因此改正第一个字段会立即清除第二个字段上的错误。结果出现时,它会重置表单:两个密码都从页面上消失,而且绝不会被写入 URL、控制台或存储。
两个模块都不发送任何内容。在实际应用中,由 submit 处理函数以 POST 提交表单,服务器响应后显示结果;第二张卡片还必须处理链接已过期的情况,显示 alert alert-danger,并提供重新申请链接的入口。没有脚本时,字段、标签和提示都在,按钮保持禁用,另有一行 <noscript> 文字给出可以写信的地址。
#无障碍
- 卡片标题就是页面的
<h1>。结果有自己的<h1>,带有tabindex="-1",焦点会移到它上面,因此页面的切换会被播报,键盘操作也从结果处继续。 - 每个字段都有可见标签。提示从一开始就在字段的
aria-describedby中;错误信息只在显示期间才加入。 - 提交失败时保留浏览器自身的行为:焦点移到第一个有误的字段,并由浏览器说明原因。
- 每条错误信息都说明该怎么做:“Use at least 12 characters.”(请至少使用 12 个字符。)“Enter the same password again.”(请再次输入相同的密码。)
- 按钮只在脚本就绪之前处于禁用状态,绝不因为表单尚未填完而禁用。
- 不要添加关于大写字母或符号的规则。让密码难以猜中的是长度,而用户看不到的规则只能靠反复试错来满足。