Pages made of components
Each page kscript make auth writes is a heading and a few components, so a form can be moved, restyled or used on another page without copying it. The controller passes what a form needs to show again, and the page hands it on:
<!-- views/auth/login.html -->
<layout path="/_layout.html" title="Sign in">
<h1>Sign in</h1>
<sign-in-form email="{$data.email ?? ''}" error="{$data.error}" />
<p><a href="/forgot-password">Forgot your password?</a> · <a href="/register">Create an account</a></p>
</layout>The component lists its inputs in construct, so kscript check reports a page that passes anything else:
<!-- _components/sign-in-form.kscript -->
<import package="KScript.Web" />
<import package="KScript.Auth" />
<type name="SignInForm" extends="Component">
<construct>
<field name="email" type="string" default="''" />
<field name="error?" type="string" />
</construct>
<fn name="render" value="```html
<status-message />
<p class="error" @if($error != null)>{$error}</p>
<form method="post" action="/login">
{! @csrf_field() }
<p><label>Email<br><input type="email" name="email" value="{$email}" required autocomplete="email"></label></p>
<p><label>Password<br><input type="password" name="password" required autocomplete="current-password"></label></p>
<p><button>Sign in</button></p>
</form>
```" />
</type>// _components/sign-in-form.kscript
import KScript.Web
import KScript.Auth
type SignInForm extends Component {
construct({ email: string = '', error?: string })
fn render() => ```html
<status-message />
<p class="error" @if($error != null)>{$error}</p>
<form method="post" action="/login">
{! @csrf_field() }
<p><label>Email<br><input type="email" name="email" value="{$email}" required autocomplete="email"></label></p>
<p><label>Password<br><input type="password" name="password" required autocomplete="current-password"></label></p>
<p><button>Sign in</button></p>
</form>
```
}| Component | What it shows |
|---|---|
<sign-in-form> |
The email and password form, and Sign in with a passkey when the site has passkeys. |
<sign-up-form> |
The name, email and password form, with the problems the SignUp type found. |
<forgot-password-form>, <reset-password-form> |
Asking for a reset link, and choosing the new password. |
<status-message> |
The message a controller flashed before its redirect. |
<account-details> |
Who is signed in, and Sign out. |
<passkey-list> |
The account's passkeys, to add or remove one. |
<api-tokens> |
The account's API tokens, to make or revoke one. |
Signing in and out changes the session cookie, so those forms post to the controller as usual. Making and revoking tokens and passkeys doesn't, so the account page's components do it themselves in live actions, and /account is a plain view route: Get(path: /account, view: 'auth/account'). Their actions are private, so nothing outside the component can call them:
<!-- _components/api-tokens.kscript -->
<import package="KScript.Web" />
<import package="KScript.Auth" />
<type name="ApiTokens" extends="Component">
<field name="name" type="string" />
<field name="shown?" type="string" private="yes" />
<fn name="make" private="yes">
<do>.shown = @api_token(@user(), name: .name->trim())</do>
<do>.name = ''</do>
</fn>
<fn name="revoke" params="id: string" private="yes">
<do>.shown = null</do>
<do>@revoke_token($id, user: @user())</do>
</fn>
<fn name="render" value="```html
<h2>API tokens</h2>
<if test="$shown != null"><p class="status">Your new token is <code>{$shown}</code>. Copy it now: it won't be shown again.</p></if>
<ul>
<for each="t" in="@api_tokens(@user())">
<li>
{$t.name}, made {$t.created_at}
<button type="button" @on-click(call: $revoke, with: [$t.id], confirm: "Revoke {$t.name}?")>Revoke</button>
</li>
</for>
</ul>
<form @on-submit($make)>
<p><label>Name<br><input @bind($name, on: action) required></label></p>
<p><button live-disable>Make a token</button></p>
</form>
```" />
</type>// _components/api-tokens.kscript
import KScript.Web
import KScript.Auth
type ApiTokens extends Component {
name: string
private shown?: string
private fn make() {
.shown = @api_token(@user(), name: .name->trim())
.name = ''
}
private fn revoke(id: string) {
.shown = null
@revoke_token($id, user: @user())
}
fn render() => ```html
<h2>API tokens</h2>
<if test="$shown != null"><p class="status">Your new token is <code>{$shown}</code>. Copy it now: it won't be shown again.</p></if>
<ul>
<for each="t" in="@api_tokens(@user())">
<li>
{$t.name}, made {$t.created_at}
<button type="button" @on-click(call: $revoke, with: [$t.id], confirm: "Revoke {$t.name}?")>Revoke</button>
</li>
</for>
</ul>
<form @on-submit($make)>
<p><label>Name<br><input @bind($name, on: action) required></label></p>
<p><button live-disable>Make a token</button></p>
</form>
```
}A live action runs the page's route and its auth middleware again, so @user() is the signed-in user and a guest can't use it. Each action carries a proof signed with the session's CSRF token, so no other site can send one.