import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Button, Card, FieldText} from '@garage44/mithril/components' import {validation, validations} from '@garage44/mithril/validation' import {$s} from '@/app' import {COMMUNITY} from '@/lib/base' import {bind, local} from '@/lib/reactive' import {forgotPassword, resetPassword, tokenFromLocation} from '@/lib/signup' /** * "Forgot your password?": an address in, a link out. * * Says the same thing whatever the address, as the server does — "no account * has that address" would tell anyone whether a person is here. The mail * names the account, for the person who forgot that too. */ export class Forgot extends MithrilComponent { s = local({busy: false, email: '', sent: false}) v = validations({ email: validation(bind(this.s, 'email')) .required('That does not look like an email address.') .check((value) => { const address = value.trim() const at = address.lastIndexOf('@') return at >= 1 && address.slice(at + 1).includes('.') }, 'That does not look like an email address.'), }) send = async (e: Event) => { e.preventDefault() const s = this.s if (s.busy || !this.v.$validate()) { return } s.busy = true s.sent = await forgotPassword(s.email.trim()) s.busy = false } view() { const s = this.s if (s.sent) { return (
If {s.email.trim()} belongs to an account here, we've sent it a link to choose a new password. It works once and expires in an hour.

Nothing arrived? It may be in a spam folder, or the account may use another address.

) } return ( // A form, so Enter or the phone's Go key sends the link.

Enter the email address of your account, and we'll send a link to choose a new password.

Signed up through Discord, Google or another site? Sign in there again instead — there is no password to reset.

) } } type Phase = 'choosing' | 'done' | 'no-token' /** * Where the link in the mail lands: choose the new password. * * Nothing is spent on opening the page — only the button does that — so a mail * scanner fetching the link changes nothing. */ export class ResetPassword extends MithrilComponent { s = local({busy: false, password: '', phase: 'choosing' as Phase, token: ''}) v = validations({ password: validation(bind(this.s, 'password')) .required() .rule((value) => { const min = $s.registration?.min_password_length ?? 0 return value.length >= min ? null : `The password needs at least ${min} characters.` }), }) oninit() { const token = tokenFromLocation(window.location.search) this.s.token = token if (!token) { this.s.phase = 'no-token' } } save = async (e: Event) => { e.preventDefault() const s = this.s if (s.busy || !this.v.$validate()) { return } s.busy = true const ok = await resetPassword(s.token, s.password) s.busy = false if (ok) { s.password = '' s.phase = 'done' } } view() { const s = this.s if (s.phase === 'done') { return (
Your new password works now, here and in the app. You've been signed out everywhere else.
) } if (s.phase === 'no-token') { return (
Open the link from the mail in full, or ask for a new one.
) } const min = $s.registration?.min_password_length return (
) } }