import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Button, Card, FieldText} from '@garage44/mithril/components' import {api} from '@/lib/api' import type {SetupInfo} from '@/lib/api' import {bind, local} from '@/lib/reactive' import {checkCaller, poll} from '@/lib/store' import {reconnect} from '@/lib/stream' /** Name, password and a code from the authenticator app (or a recovery code). */ export class Login extends MithrilComponent { s = local({name: '', password: '', code: '', error: '', busy: false}) async submit(e: Event) { e.preventDefault() this.s.busy = true this.s.error = '' try { await api.login(this.s.name.trim(), this.s.password, this.s.code.trim()) this.s.password = '' this.s.code = '' await checkCaller() await poll() reconnect() } catch (err) { this.s.error = (err as Error).message } finally { this.s.busy = false } } view() { return ( void this.submit(e)}> {this.s.error ? {this.s.error} : null} Log in No account? An admin makes one with midas account add <name> and sends you its setup link. ) } } /** * The setup link's page (`/setup/`, from `midas account add` or * `reset`): choose a password, add the secret to an authenticator app, prove * it with a first code, and keep the recovery codes, shown this once. */ export class Setup extends MithrilComponent<{token: string}> { s = local({ info: null as SetupInfo | null, password: '', again: '', code: '', error: '', busy: false, recovery: [] as string[], }) oninit(vnode: m.Vnode<{token: string}>) { api.setupInfo(vnode.attrs.token) .then((info) => (this.s.info = info)) .catch((e: Error) => (this.s.error = e.message)) } async submit(e: Event, token: string) { e.preventDefault() if (this.s.password !== this.s.again) { this.s.error = 'the two passwords differ' return } this.s.busy = true this.s.error = '' try { this.s.recovery = (await api.setupComplete(token, this.s.password, this.s.code.trim())).recovery this.s.password = this.s.again = '' } catch (err) { this.s.error = (err as Error).message } finally { this.s.busy = false } } view(vnode: m.Vnode<{token: string}>) { const info = this.s.info if (this.s.recovery.length) { return ( {this.s.recovery.map((c) => ( {c} ))} { // Off the setup link, and loaded again: the login. location.replace('/') }} > I have kept them — log in ) } if (!info) return this.s.error ? {this.s.error} : Opening the setup link… return ( void this.submit(e, vnode.attrs.token)}> 1. Choose a password, at least twelve characters. 2. Add this account to an authenticator app: scan the code, or type the secret. {m.trust(info.qr_svg)} secret {info.secret} 3. Type the code the app shows now. {this.s.error ? {this.s.error} : null} Finish ) } }
No account? An admin makes one with midas account add <name> and sends you its setup link.
midas account add <name>
{c}
Opening the setup link…
1. Choose a password, at least twelve characters.
2. Add this account to an authenticator app: scan the code, or type the secret.
secret {info.secret}
{info.secret}
3. Type the code the app shows now.