KScript docs
EN

Sign-in: KScript.Auth

Guests and owners

Some apps let people use them before they sign up: a board of notes, a basket, a draft. KScript.Auth gives every browser a guest id, and the Ownable mixin keeps each row to whoever made it, guest or user.

<import package="KScript.Data" />
<import package="KScript.Auth" />
<import package="KScript.Http" />

<model name="Note" with="[Ownable, Timestamps]" description="A note on someone's board.">
  <field name="title" type="string" />
</model>

<Routes>
  <Get path="/notes">
    <return value="Note->mine()->order_by(id)->get()" />
  </Get>
  <Post path="/notes">
    <return value="Note->create(title: $json.title)" />
  </Post>
  <Get path="/notes/{id}">
    <return value="Note->mine()->find_or_fail($id)" />
  </Get>
</Routes>
import KScript.Data
import KScript.Auth
import KScript.Http

/// A note on someone's board.
model Note extends [Ownable, Timestamps] {
  title: string
}

Routes {
  Get(path: /notes) { return Note->mine()->order_by(id)->get() }
  Post(path: /notes) { return Note->create(title: $json.title) }
  Get(path: /notes/{id}) { return Note->mine()->find_or_fail($id) }
}

Ownable comes with KScript.Auth, so there is nothing to declare: importing the package is enough. Its owner is a relation, so it is typed: $note.user is a User record, and ->with(user) and ->where(user: $ada) work as they do for any relation. It adds:

Member What it does
user?: User The user the row belongs to, or null when a guest made it. It is stored as user_id, a foreign key to users, so deleting a user deletes their rows.
guest?: Guest The guest the row belongs to, or null when a signed-in user made it. It is stored as guest_id.
->mine(as?:) A scope: the rows of whoever is making the request. as: names a user instead.
->is_mine() Whether this record belongs to whoever is making the request.

Exactly one of user and guest is set. Note->create() fills it in itself, so a request can't make a row for someone else: giving another user: or a guest: is a Forbidden error. Neither can be changed once the row is made (->update(user:) is refused). Outside a request (a seeder, a job, kscript run), nobody is making the request, so name the owner: Note->create(title: 'Welcome', user: $ada) and Note->mine(as: $ada). Without one, both are Unauthorized errors.

Ownable's user is a record of the model the users are kept in, so an app with Ownable models keeps its users in a model: "users": "User" in http.auth, which kscript make auth sets for you (see Users in a model). Without it, making a row as a signed-in user is a Config error that says so.

  • @guest() is this browser's guest id: the key of its Guest, a uuid unless your copy of Guest says otherwise. The first call makes one and sends it in the ks_guest cookie (http.auth.guest_cookie), which lasts http.auth.guest_lifetime (a year). It is null when someone is signed in, and outside a request. A guest is stored in the guests table the first time it owns a row.

Changing User, Guest and Ownable

User, Guest and Ownable are KScript models that come with KScript.Auth. To change one, copy it into your project with kscript publish, then edit the copy. The copy replaces KScript.Auth's model everywhere: in scripts, routes, tests, migrations and the editor.

Shell
kscript publish          # list the models the packages ship
kscript publish User     # copy User to models/user.kscript
kscript publish Guest Ownable

The relations follow the models. Give your User a uuid key and Ownable's user is stored as a uuid too:

<import package="KScript.Data" />

<model name="User" key="uid" auto_create="yes" unique="[email]" hidden="[password]" with="Timestamps">
  <field name="uid" type="uuid" default="@uuid()" />
  <field name="name" type="string" />
  <field name="email" type="email" />
  <field name="password" type="password" />
</model>
import KScript.Data

model User(key: 'uid', auto_create, unique: [email], hidden: [password]) extends Timestamps {
  uid: uuid = @uuid()
  name: string
  email: email
  password: password
}

Guests work the same way. Guest's key is id: uuid = @uuid(), made in the browser's cookie and stored when the guest first owns something. Leave the key out of your copy, and guests are numbered by the database instead: a new guest is stored at once, to get its number. Fields you add to Guest are there for each guest, such as a display name.

Keeping a guest's rows when they sign up

With "adopt_guest": true in http.auth, signing in (with @login, @attempt or a passkey) moves the guest's rows to their account. It happens once per model, the first time each model is used after sign-in, and includes soft-deleted rows. The guest cookie is cleared. With it off (the default), a guest's rows stay theirs, and the account starts empty.

Testing as a guest

@acting_as_guest(id?:) makes the rest of the test run as a guest, in the test and in the routes it calls with @api(/...). It returns the guest's id: a new one each call, or the one you give it. @acting_as(...) switches back to a user.

Acting as a guest stands in for the guest cookie, not for signing in. A request that sends the session cookie of someone who signed in runs as them, as it would in a browser, so a test can follow a guest through sign-in:

<import package="KScript.Auth" />
<import package="KScript.Net" />

<test name="a guest keeps their notes when they sign in">
  <do>@register('bea@example.test', 'correct-horse-battery-9', name: 'Bea')</do>
  <do>@acting_as_guest()</do>
  <do>@api('/api/notes', post: { title: 'milk' })</do>
  <let name="signed_in" value="@api('/api/signin', post: { email: 'bea@example.test' }).cookies.ks_session" />
  <let name="notes" value="@api('/api/notes', options: { headers: { Cookie: &quot;ks_session={ $signed_in }&quot; } })" />
  <expect value="$notes->convert(from: json)" equals="['milk']" />
</test>
import KScript.Auth
import KScript.Net

test(name: 'a guest keeps their notes when they sign in') {
  @register('bea@example.test', 'correct-horse-battery-9', name: 'Bea')
  @acting_as_guest()
  @api('/api/notes', post: { title: 'milk' })
  $signed_in = @api('/api/signin', post: { email: 'bea@example.test' }).cookies.ks_session
  $notes = @api('/api/notes', options: { headers: { Cookie: "ks_session={ $signed_in }" } })
  expect($notes->convert(from: json), equals: ['milk'])
}
<import package="KScript.Auth" />
<import package="KScript.Net" />

<test name="guests see only their own notes">
  <do>@acting_as_guest()</do>
  <do>@api('/api/notes', post: { title: 'milk' })</do>
  <do>@acting_as_guest()</do>
  <expect value="@api('/api/notes')->convert(from: json)" equals="[]" />
</test>
import KScript.Auth
import KScript.Net

test(name: 'guests see only their own notes') {
  @acting_as_guest()
  @api('/api/notes', post: { title: 'milk' })
  @acting_as_guest()
  expect(@api('/api/notes')->convert(from: json), equals: [])
}

Edit this page on GitHub