import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Button, Card, FieldText} from '@garage44/mithril/components' import type {AccountSignin, LinkedIdentity} from '@/types' import {$s} from '@/app' import {ProviderLogo} from '@/components/elements/providers/providers' import {begin, removeIdentity, setPassword} from '@/lib/auth' import {Icon} from '@/lib/icons' import {bind, local} from '@/lib/reactive' import {asksServer, canRemove, confirmLabel, linkable, reauthWith} from '@/lib/signin' interface WaysAttrs { /** Fetch the account's ways in again, after anything here changed them. */ reload: () => Promise signin: AccountSignin } /** The account page's path, where every round trip from here comes back to. */ const HERE = '/tokens' /** * Confirm with a provider: the round trip that stands in for "type your * password again" on an account that has none. Mastodon asks which server * first, as it does on the sign-in card. */ export function confirmWith(identity: Pick) { if (asksServer(identity)) { m.route.set('/signin/fediverse', {purpose: 'reauth', return: HERE}) return } void begin(identity.provider, 'reauth', {return: HERE}) } /** * "Ways to sign in" (`docs/mockups/external-sign-in/v1.html`, section 5): each * linked provider, the ones that could be linked, and the password. * * Never zero ways in: the last one cannot be removed. The server refuses it * too, and its sentence is shown when it does; this greys the button and says * why before anybody presses it. */ export class Ways extends MithrilComponent { s = local({ busy: false, current: '', /** The password form: closed, adding, or changing. */ editing: '' as '' | 'add' | 'change', error: '', next: '', /** The server wants a confirmation through a provider before a first password. */ reauth: false, }) remove = async (identity: LinkedIdentity, reload: () => Promise) => { if (!confirm(`Stop signing in with ${identity.provider_name}?`)) return this.s.busy = true if (await removeIdentity(identity.id, identity.provider_name)) await reload() this.s.busy = false } savePassword = async (e: Event, attrs: WaysAttrs) => { e.preventDefault() const s = this.s if (s.busy || !s.next) return s.busy = true s.error = '' s.reauth = false const result = await setPassword(s.next, s.editing === 'change' ? s.current : undefined) s.busy = false s.current = '' if (result === 'ok') { s.next = '' s.editing = '' await attrs.reload() } else if (result === 'reauth') { s.reauth = true } else { s.error = result } } view(vnode: m.Vnode) { const s = this.s const {signin, reload} = vnode.attrs const removable = canRemove(signin) const first = reauthWith(signin.identities) const min = $s.registration?.min_password_length ?? 0 return (
    {signin.identities.map((identity) => (
  • {ProviderLogo({id: identity.provider, kind: identity.kind}, 20)}
    {identity.provider_name} {identity.handle} ยท since{' '} {new Date(identity.linked_at * 1000).toLocaleDateString(undefined, { day: 'numeric', month: 'short', year: 'numeric', })}
  • ))} {linkable(signin).map((p) => (
  • {ProviderLogo(p, 20)}
    {p.name} Not linked
  • ))}
  • {Icon('lock')}
    Password {signin.has_password ? 'Set' : first ? `None. You sign in with ${first.provider_name}.` : 'None.'}
    {!s.editing && ( )}
{!removable && signin.identities.length === 1 && ( {signin.identities[0].provider_name} is your only way in, so it can't be removed until you link another or add a password. )} {s.editing && (
void this.savePassword(e, vnode.attrs)}> {s.editing === 'change' && ( )} {s.reauth && first && ( Confirm it's you with {first.provider_name} first, then add the password here. )} {s.error && {s.error}}
{s.reauth && first ? ( ) : ( )}
)}
) } }