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 {ProviderChoices} from '@/components/elements/providers/providers' import {ADMIN} from '@/lib/base' import {bind, local} from '@/lib/reactive' import {register, whatIsMissing} from '@/lib/signup' /** * Make an account. * * Everything about this screen is arranged around one fact: the account is not * usable when the form is submitted. So there is no "you're in" and no redirect * to a signed-in page — it swaps itself for the "check your inbox" panel, which * is the actual state of the world and the only place the re-send button makes * sense. */ export class Register extends MithrilComponent { s = local({busy: false, email: '', password: '', sent: false, username: ''}) /* * The per-field half of `whatIsMissing`, with its sentences, so each problem * shows under the field it is about. `whatIsMissing` stays the submit gate * (it is the tested copy of the server's rules); these only decide where the * sentence goes. The limits are read from `$s.registration` when a rule runs, * not when it is built, because the rules arrive after this component may * have been constructed. */ v = validations({ email: validation(bind(this.s, 'email')) .required('That does not look like an email address.') .check((value) => { // Deliberately shallow — see `whatIsMissing`. const address = value.trim() const at = address.lastIndexOf('@') return at >= 1 && address.slice(at + 1).includes('.') }, 'That does not look like an email address.'), 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.` }), username: validation(bind(this.s, 'username')) .required('Pick a username.') .rule((value) => { const max = $s.registration?.max_username_length ?? Infinity return value.trim().length <= max ? null : `That username is longer than ${max} characters.` }), }) view() { const s = this.s const rules = $s.registration if (rules === undefined) { return (

Asking the server whether it takes registrations…

) } if (!rules) { return (
This server did not answer when we asked whether it takes registrations. It may be down, or older than this page.
) } if (!rules.open) { return (
This server does not take registrations — accounts on it are made by its operator.
) } if (s.sent) { return (
We've sent a link to {s.email.trim()}. Open it to finish signing up — it works once and expires in {rules.verification_valid_hours} hours.

Nothing arrived? It may be in a spam folder.{' '} Send another.

) } const missing = whatIsMissing( s.username, s.email, s.password, rules.min_password_length, rules.max_username_length, ) const submit = async (e: Event) => { e.preventDefault() if (!this.v.$validate() || missing || s.busy) { return } s.busy = true const result = await register(s.username.trim(), s.email.trim(), s.password) s.busy = false if (result.ok) { // Out of the component's state as well as off the screen. It is the // one field here worth not leaving in a form somebody may walk away // from. s.password = '' s.sent = true } } return ( // A real
, so the phone keyboard shows a Go key and Enter submits.

One account for the world and for this page. We'll send a link to confirm your address before it works.

{/* Signing up through a provider is signing in: /welcome asks for the name. */} {/* * Errors wait until a field is left or the form is submitted * (the library's rule, and the one this form had): on an * untouched form "Pick a username" is not advice, it is the form * telling somebody off for not having filled it in yet. */}
) } }