KScript docs
EN

Sign-in: KScript.Auth

Protecting routes

To let only signed-in people reach a route, put it behind the auth middleware. The auth middleware lets signed-in users through and sets $request.user. A browser is sent to http.auth.login (/login) and comes back afterwards. A request that wants JSON (an API route, an Authorization header, or Accept: application/json) gets a 401 instead. The guest middleware is the other way round: it sends people who are signed in to http.auth.home.

<KScript.Routes>
  <import package="KScript.Auth" />
  <import path="controllers/accounts.kscript" as="accounts" />

  <Group middleware="guest">
    <Get path="/login" to="accounts.show_login" name="login" />
    <Post path="/login" to="accounts.sign_in" />
  </Group>
  <Group middleware="auth">
    <Get path="/account" view="auth/account" />
    <Post path="/logout" to="accounts.sign_out" />
  </Group>
</KScript.Routes>
import KScript.Http
import KScript.Auth
import "controllers/accounts.kscript" as accounts

Routes {
  Group(middleware: guest) {
    Get(path: /login, to: @accounts.show_login, name: 'login')
    Post(path: /login, to: @accounts.sign_in)
  }
  Group(middleware: auth) {
    Get(path: /account, view: 'auth/account')
    Post(path: /logout, to: @accounts.sign_out)
  }
}
Middleware Lets through
auth A user signed in by the session or an API token, or the shared API_TOKEN (see Built-in middleware).
auth:session Only a user signed in by the session.
auth:token Only a user's API token. It always answers 401, never a redirect.
auth:ADMIN_TOKEN Only the bearer token in that environment variable, as before KScript.Auth.
guest Only people who aren't signed in. Others are redirected home, or get a 403 when they want JSON.
csrf Only requests with the session's CSRF token. Web routes get it on their own (see below).

Edit this page on GitHub