コンポーネントで構成されるページ
kscript make authが作成する各ページは、見出しといくつかのコンポーネントで構成されています。そのため、フォームはコピーせずに移動、スタイル変更、別のページでの再利用が可能です。コントローラーはフォームの再表示に必要な情報を渡し、ページはそれを次へ受け渡します。
<!-- 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>コンポーネントは入力をconstructに列挙するため、kscript checkは、それ以外のものを渡すページを報告します。
<!-- _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>
```
}| コンポーネント | 表示内容 |
|---|---|
<sign-in-form> |
メールアドレスとパスワードのフォーム。サイトでパスキーが有効な場合はパスキーでサインインも表示されます。 |
<sign-up-form> |
名前、メールアドレス、パスワードのフォーム。SignUp型が検出した問題も表示されます。 |
<forgot-password-form>, <reset-password-form> |
リセットリンクの要求と、新しいパスワードの設定。 |
<status-message> |
リダイレクト前にコントローラーが表示したメッセージ。 |
<account-details> |
サインイン中のユーザーとサインアウト。 |
<passkey-list> |
アカウントのパスキー。追加または削除できます。 |
<api-tokens> |
アカウントのAPIトークン。作成または取り消しができます。 |
サインイン・サインアウトではセッションクッキーが変わるため、それらのフォームは通常どおりコントローラーに送信します。トークンやパスキーの作成・取り消しはそうではないため、アカウントページのコンポーネントがライブアクションで自ら処理します。/accountは単純なビュールートで、Get(path: /account, view: 'auth/account')です。そのアクションはprivateなので、コンポーネントの外からは呼び出せません。
<!-- _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>
```
}ライブアクションはページのルートと、そのauthミドルウェアを再度実行します。そのため@user()はサインイン中のユーザーであり、ゲストは使用できません。各アクションには、セッションのCSRFトークンで署名された証明が付与されるため、他のサイトから送信することはできません。