Chapter 4

Tutorial: build a sign-in form

This walks through a complete form in a fresh entry file. The demo in src/demo/main.ts is a larger version of the same pattern.

4.1 Page shell#

index.html needs a full-size host element and must load your module:

<main id="app" aria-label="Sign in"></main>
<script type="module" src="/src/signin.ts"></script>

public/style.css already makes #app and its canvas fill the viewport and disables touch callouts.

4.2 Create the canvas application and the UI runtime#

// src/signin.ts
import { Application, FormUI, lightTheme, darkTheme } from './lib';

const host = document.getElementById('app')!;
const theme = matchMedia('(prefers-color-scheme: dark)').matches ? darkTheme : lightTheme;

const app = new Application();
await app.init({
  resizeTo: host,
  background: theme.colors.background,
  antialias: true,
  autoDensity: true,                                  // CSS pixels = layout units
  resolution: Math.min(window.devicePixelRatio || 1, 2),
});
host.appendChild(app.canvas);

const ui = new FormUI(app, { theme, host });
app.stage.addChild(ui);

4.3 Add controls#

import { TextInput, PasswordInput, Checkbox, Button } from './lib';

const email = new TextInput(ui, {
  name: 'email',
  label: 'Email',
  placeholder: 'you@example.com',
  required: true,
  inputMode: 'email',
  maxLength: 254,
});
const password = new PasswordInput(ui, { name: 'password', label: 'Password', required: true });
const remember = new Checkbox(ui, { name: 'remember', label: 'Keep me signed in' });
const signIn = new Button(ui, { name: 'signIn', text: 'Sign in' });

4.4 Lay them out#

import { Column, ScrollView, insets } from './lib';

const column = new Column([email, password, remember, signIn], 20, insets(24));
const view = new ScrollView(ui, column);
ui.content.addChild(view);

const fit = () => view.setViewport(app.screen.width, app.screen.height);
app.renderer.on('resize', fit);
fit();

To centre a fixed-width card instead, wrap the column (see the Card/Page classes in src/demo/main.ts).

4.5 Wire up the form and submit securely#

import { Form, Toast, sealSecret, secureSubmit, type Secret, type EncryptionKey } from './lib';

const session: { csrfToken: string; encryption: EncryptionKey } =
  await (await fetch('/api/session', { cache: 'no-store' })).json();
const toast = new Toast(ui);

const form = new Form(ui, {
  onSubmit: async (values) => {
    const envelope = await sealSecret(values.password as Secret, session.encryption, 'password');
    const res = await secureSubmit(
      '/api/login',
      { email: values.email, remember: values.remember, password: envelope },
      { csrfToken: session.csrfToken },
    );
    const body = await res.json();
    if (body.challenge) session.encryption.challenge = body.challenge;   // single-use: always refresh
    return { ok: res.ok, message: body.message, fieldErrors: body.fieldErrors };
  },
});

form.add(email, password, remember).bindSubmit(signIn);
form.on('success', (r) => toast.show(r.message ?? 'Welcome back!', 'success'));
form.on('failure', (r) => toast.show(r.message ?? 'Sign-in failed.', 'error'));

What you get without writing more code:

The server side of /api/login is shown in §14.4.