Signing up and in
A controller imports KScript.Auth. @register adds a user, @attempt checks an email and password and signs them in, and @logout signs them out.
<!-- requests/sign-up.kscript -->
<type name="SignUp">
<field name="name" type="string" />
<field name="email" type="email" />
<field name="password" type="password" />
</type>// requests/sign-up.kscript
type SignUp {
name: string
email: email
password: password
}<!-- controllers/accounts.kscript -->
<import package="KScript.Http" />
<import package="KScript.Auth" />
<import path="../requests/sign-up.kscript" />
<fn name="sign_up" params="form: map">
<let name="data" value="SignUp($form)" />
<let name="user" value="@register($data.email, $data.password, name: $data.name)" />
<do>@login($user)</do>
<return value="@redirect(@intended())" />
</fn>
<fn name="sign_in" params="form: map">
<let name="user" value="@attempt($form.email, $form.password, remember: $form.remember == 'yes')" />
<if $user == null>
<return value="@view('auth/login', data: { error: "That email and password don't match an account." }, status: 422)" />
</if>
<return value="@redirect(@intended())" />
</fn>
<fn name="sign_out">
<do>@logout()</do>
<return value="@redirect(/)" />
</fn>// controllers/accounts.kscript
import KScript.Http
import KScript.Auth
import "../requests/sign-up.kscript"
fn sign_up(form: map) {
$data = SignUp($form)
$user = @register($data.email, $data.password, name: $data.name)
@login($user)
return @redirect(@intended())
}
fn sign_in(form: map) {
$user = @attempt($form.email, $form.password, remember: $form.remember == 'yes')
if $user == null {
return @view('auth/login', data: { error: "That email and password don't match an account." }, status: 422)
}
return @redirect(@intended())
}
fn sign_out() {
@logout()
return @redirect(/)
}@register(email, password, fields...: User)checks the email and password with their types and your project's rules. It refuses an email someone already signed up with (case doesn't matter), keeps only an argon2id hash of the password, and keeps the other named values as the user's fields. A problem is aValidationerror that names the field, which a route answers with 422.@attempt(email, password, remember?, session?)returns the user, ornull. When the email has no account it takes about as long, so the timing doesn't give away who has one. A hash made with older settings is made again, with the algorithm the project's password type names (argon2id unless it saysbcrypt).session: falseonly checks, for an API that answers with a token.@login(user, remember?)signs a user in. The session gets a new id, so an id seen before the sign-in can't be used.remember: truekeeps them signed in forhttp.auth.remember(30 days) after the browser closes.@logout(everywhere?)ends the session and removes its cookie.everywhere: trueends the user's sessions on every device.@user()is the signed-in user (a map withid,email,created_atand their fields, never the hash), ornull.@is_signed_in()is true when there is one.@intended(default?)is the page theauthmiddleware sent someone away from, or the default (http.auth.home,/). It is always a path on this site, so it can't send people elsewhere.
Failed sign-ins
After http.auth.attempts (5) failures for the same email and address within http.auth.lockout (a minute), @attempt throws a TooManyRequests error that says how long to wait. A route answers it with 429 and a Retry-After header. Catch it to show the form again:
<fn name="sign_in" params="form: map">
<let name="user" value="null" />
<try>
<set name="user" value="@attempt($form.email, $form.password)" />
<catch as="err" when="$err.type == 'TooManyRequests'">
<return value="@view('auth/login', data: { error: $err.message }, status: 429)" />
</catch>
</try>
<return value="@redirect(@intended())" />
</fn>fn sign_in(form: map) {
$user = null
try {
$user = @attempt($form.email, $form.password)
} catch err when $err.type == 'TooManyRequests' {
return @view('auth/login', data: { error: $err.message }, status: 429)
}
return @redirect(@intended())
}