chadtmiller.com/monolith
An atproto PDS
0
0
<script lang="ts">
import type { Credentials } from '#lib/api';
import {
authorizeData,
errorData,
initialSessions,
passkeyData,
type Account,
type Known
} from '#lib/data';
import AddPasskey from './views/AddPasskey.svelte';
import ChooseAccount from './views/ChooseAccount.svelte';
import Consent from './views/Consent.svelte';
import EmailCode from './views/EmailCode.svelte';
import ErrorPage from './views/ErrorPage.svelte';
import PasskeyAdded from './views/PasskeyAdded.svelte';
import SignIn from './views/SignIn.svelte';
// The views shown, the first at the bottom: `Back` from Sign in or
// Email code shows the one under the top.
type View =
| { kind: 'sign-in' }
| { kind: 'email-code' }
| { kind: 'choose' }
| { kind: 'consent'; account: Account; token?: string };
// The accounts the page knows, as Choose an account shows them: those of
// `__sessions`, and each signed in on this page since it loaded.
let known = $state.raw<Known[]>(initialSessions);
// Whether the identifier is the client's `loginHint`, which Sign in shows read-only.
let hinted = $state(!!authorizeData?.loginHint);
let form = $state<Credentials>({
identifier: authorizeData?.loginHint ?? '',
password: '',
remember: false
});
function first(): View {
const selected = initialSessions.find(
(s) => s.account.did === authorizeData?.selectedDid && !s.loginRequired
);
if (selected) return { kind: 'consent', account: selected.account };
if (authorizeData?.loginHint) return { kind: 'sign-in' };
if (initialSessions.length) return { kind: 'choose' };
return { kind: 'sign-in' };
}
let views = $state.raw<View[]>([first()]);
const view = $derived(views[views.length - 1]);
const reached = $derived(views.length > 1);
// The passkey page's: whether its passkey was added.
let added = $state(false);
const go = (v: View) => (views = [...views, v]);
const back = () => (views = views.slice(0, -1));
function signInAs(next: Credentials) {
hinted = false;
form = next;
go({ kind: 'sign-in' });
}
function signedIn(account: Account, token?: string) {
known = [
...known.filter((k) => k.account.did !== account.did),
{ account, loginRequired: false, token }
];
go({ kind: 'consent', account, token });
}
function choose(k: Known) {
if (k.loginRequired) signInAs({ identifier: k.account.handle, password: '', remember: true });
else go({ kind: 'consent', account: k.account, token: k.token });
}
</script>
{#if errorData}
<ErrorPage data={errorData} />
{:else if passkeyData}
{#if added}
<PasskeyAdded account={passkeyData.account} />
{:else}
<AddPasskey data={passkeyData} onadded={() => (added = true)} />
{/if}
{:else if authorizeData}
{#key view}
{#if view.kind === 'sign-in'}
<SignIn
bind:form
{hinted}
{reached}
onback={back}
onsignedin={signedIn}
onfactor={() => go({ kind: 'email-code' })}
/>
{:else if view.kind === 'email-code'}
<EmailCode {form} onback={back} onsignedin={signedIn} />
{:else if view.kind === 'choose'}
<ChooseAccount
{known}
onchoose={choose}
onanother={() => signInAs({ identifier: '', password: '', remember: false })}
/>
{:else}
<Consent
data={authorizeData}
account={view.account}
token={view.token}
{reached}
onchoose={() => go({ kind: 'choose' })}
/>
{/if}
{/key}
{/if}