KScript docs
EN

Sign-in: KScript.Auth

Email verification

Many apps want to know that people own the address they signed up with. KScript.Auth checks it with a signed link: you email the link, and following it marks the user as verified.

@verification_token(user, expires?) makes a token for the link. It is signed with the app's secret (KSCRIPT_SECRET, like @signed_url), names the user and their current email, and works for an hour unless expires says otherwise. Nothing is stored, so there is nothing to clean up, and changing the email makes old links stop working.

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

<fn name="send_verification" params="user: map, request: map">
  <let name="link" value="'https://' + $request.headers.host + @route('verification.verify', token: @verification_token($user, expires: 24h))" />
  <do>@send_mail(to: $user, subject: 'Verify your email address', view: 'mail/verify-email', data: { name: $user.name, link: $link })</do>
</fn>
import KScript.Http
import KScript.Auth
import KScript.Mail

fn send_verification(user: map, request: map) {
  $link = 'https://' + $request.headers.host + @route('verification.verify', token: @verification_token($user, expires: 24h))
  @send_mail(to: $user, subject: 'Verify your email address', view: 'mail/verify-email', data: { name: $user.name, link: $link })
}

@verify_email(token) finishes it. It sets the user's email_verified_at to now and returns the user, or returns null when the token is wrong, has run out, or was made for an email the user no longer has:

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

<fn name="verify" params="token: string">
  <let name="user" value="@verify_email($token)" />
  <if $user == null>
    <return value="@view('auth/verify-email', data: { error: 'That link has expired. Ask for a new one.' }, status: 422)" />
  </if>
  <return value="@redirect(/account)" />
</fn>
import KScript.Http
import KScript.Auth

fn verify(token: string) {
  $user = @verify_email($token)
  if $user == null {
    return @view('auth/verify-email', data: { error: 'That link has expired. Ask for a new one.' }, status: 422)
  }
  return @redirect(/account)
}

The verified middleware lets only verified users through. A guest is treated as the auth middleware treats one. A signed-in user who hasn't verified is sent to the route named verification.notice when there is one, and gets a 403 otherwise (API requests always get the 403):

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

  <Group middleware="auth, verified">
    <Get path="/billing" view="billing" />
  </Group>
</KScript.Routes>
import KScript.Http
import KScript.Auth

Routes {
  Group(middleware: [auth, verified]) {
    Get(path: /billing, view: 'billing')
  }
}

kscript make auth sets all of this up: sign-up emails the link, /email/verify/{token} (named verification.verify) checks it, /email/verify (named verification.notice) asks people to look for the email, and posting to /email/verification-notification sends it again.

Users have an email_verified_at field, null until they verify. The User model KScript.Auth ships has it as email_verified_at?: date. If you published User before it had the field, add that line to your copy and run kscript db diff to write the migration.

Edit this page on GitHub