KScript docs
EN

Sign-in: KScript.Auth

Users

@users() is the app's users. They are kept as JSON files in the app's data folder (see App folders), or in http.auth.folder, or in a model (see Users in a model).

Method What it does
->find(id) The user with this id, or null.
->find_email(email) The user with this email (case doesn't matter), or null.
->all() Every user, oldest first.
->count() How many users there are.
->verify(email, password) The user with this email and password, or null. Unlike @attempt, it doesn't sign them in or count failures.
->update(id, fields...: User) Changes a user's fields (null removes one). A new email is checked like @register's.
->set_password(id, password) Gives a user a new password, checked by the password type.
->delete(id) Deletes a user with their sessions, API tokens and reset tokens.

Users from a database of your own work too: @login({ id: $row.id, email: $row.email }) signs in any map with an id, and the session keeps a copy of it.

Users in a model

To keep the users in the app's database, name a KScript.Data model in http.auth.users. kscript make auth does this for you:

JSON
{ "http": { "auth": { "users": "User" } } }

KScript.Auth comes with a User model: a name, an email and a password, with timestamps, in a users table it makes the first time it's used (auto_create). kscript make auth (or kscript publish User) copies it to models/user.kscript, where it is yours to change: add fields, relations or a key of your own. Any model in the models folder works too. It needs an email field and a password field, and any other fields you give @register or ->update:

<import package="KScript.Data" />

<model name="User" unique="[email]" hidden="[password]" with="Timestamps">
  <field name="name" type="string" />
  <field name="email" type="email" />
  <field name="password" type="password" />
  <field name="posts" type="Post[]" />
</model>
import KScript.Data

model User(unique: [email], hidden: [password]) extends Timestamps {
  name: string
  email: email
  password: password
  posts: Post[]
}

The other named values of @register and ->update are the model's fields, so the editor completes them and kscript check reads them against the model: a name it doesn't have (nmae: 'Bea') is an error that suggests the closest one, and a value that can't be the field's type is an error too. User in their signatures stands for whichever model http.auth.users names.

When you move to migrations, take auto_create off and let kscript db diff write the table.

Then a user is a row of the users table, and @user(), @register and @users() give you the model's record, so its relations and methods work: @user().posts. The password field holds the hash KScript.Auth makes; hidden: [password] keeps it out of JSON. Ids are the model's keys. Sessions, API tokens and reset tokens are still files in the app's state folder.

With passkey_signup on, someone who signs up with a passkey becomes a record with no email or password, so the model's email and password must be optional (email?: email, password?: password). kscript make auth --passkeys writes them that way. Their passkeys are kept with the auth data, in the _passkeys folder.