KScript 文档
ZH

登录:KScript.Auth

由组件构成的页面

每个页面 kscript make auth 编写的内容都由一个标题和几个组件组成,因此表单可以被移动、重新设计或用在其他页面上,而无需复制。控制器传递表单再次显示所需的内容,页面再将其转交下去:

HTML 页面
<!-- 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
      &lt;status-message />
      &lt;p class=&quot;error&quot; @if($error != null)>{$error}&lt;/p>
      &lt;form method=&quot;post&quot; action=&quot;/login&quot;>
        {! @csrf_field() }
        &lt;p>&lt;label>Email&lt;br>&lt;input type=&quot;email&quot; name=&quot;email&quot; value=&quot;{$email}&quot; required autocomplete=&quot;email&quot;>&lt;/label>&lt;/p>
        &lt;p>&lt;label>Password&lt;br>&lt;input type=&quot;password&quot; name=&quot;password&quot; required autocomplete=&quot;current-password&quot;>&lt;/label>&lt;/p>
        &lt;p>&lt;button>Sign in&lt;/button>&lt;/p>
      &lt;/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 令牌,用于创建或撤销令牌。

登录和退出会更改会话 Cookie,因此这些表单会像往常一样提交到控制器。创建和撤销令牌及通行密钥则不同,账户页面的组件会通过 实时操作 自行处理,而 /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
      &lt;h2>API tokens&lt;/h2>
      &lt;if test=&quot;$shown != null&quot;>&lt;p class=&quot;status&quot;>Your new token is &lt;code>{$shown}&lt;/code>. Copy it now: it won't be shown again.&lt;/p>&lt;/if>
      &lt;ul>
        &lt;for each=&quot;t&quot; in=&quot;@api_tokens(@user())&quot;>
          &lt;li>
            {$t.name}, made {$t.created_at}
            &lt;button type=&quot;button&quot; @on-click(call: $revoke, with: [$t.id], confirm: &quot;Revoke {$t.name}?&quot;)>Revoke&lt;/button>
          &lt;/li>
        &lt;/for>
      &lt;/ul>
      &lt;form @on-submit($make)>
        &lt;p>&lt;label>Name&lt;br>&lt;input @bind($name, on: action) required>&lt;/label>&lt;/p>
        &lt;p>&lt;button live-disable>Make a token&lt;/button>&lt;/p>
      &lt;/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 令牌签名的凭证,因此其他网站无法发送此类操作。