These guides are primarily for agent readers. They explain how agents use Sails. If you are a person, start at sails.app or Add Sails.

Add sign-in to a site

Published sites get /auth/login, /auth/callback, and /auth/logout on the same origin as the mount. OAuth client secrets stay on the platform. Do not register custom OAuth apps or put session keys in workspace files.

Done looks like

A visitor clicks Sign in, finishes login, and returns to your page with a sails_session cookie on that host. A handler with auth: required refuses unsigned POSTs with 401. request auth sub in the handler prints a platform identity UUID.

Worked example: public site, signed-in form

Job: ship a public demo, protect one POST handler, and read who submitted.

Run this from your home folder. ship create --path is relative to the repository root, not your current folder.

cd ~
cp -r /templates/static-site-demo ./site
ship create site --policy 644

The default policy is 600 (owner only), so a public site needs --policy 644. Sign-in is then only required where you set it.

The template handler pages/contacts/create.api.sh looks like this (body schema trimmed):

---
methods: [POST]
auth: required
security: definer
redirect: /contacts
---
#!/usr/bin/env bash
set -euo pipefail

name="$(request form name)"
sub="$(request auth sub)"
# … write a page or row keyed by $sub under pages/contacts/ …
respond redirect /contacts

security: definer matters here. Handlers run as the visitor by default (invoker), and a signed-in visitor who is not a repository member cannot write your files. auth: required checks sign-in only. It does not grant write access. Only repository owners can save a handler that declares security: definer.

On a page, link to platform routes (same host as the mount):

<a href="/auth/login?return_to=/contacts">Sign in</a>
<a href="/auth/logout">Sign out</a>

Use /auth/login, not /auth/sign-in or a custom OAuth URL. On the live mount, /auth/login answers 303 to the platform sign-in page with your return_to kept. /auth/logout answers 303 back to /.

What an unsigned POST gets

A signed-out POST to an auth: required handler does not redirect. Checked on a live 644 mount:

POST /contacts/create  (form, signed out)  ->  401  sign in required
POST /contacts/create  (JSON, signed out)  ->  401  {"error":"sign in required"}

The visitor sees a bare error page. Put a Sign in link next to the form, or send them to login yourself with auth: optional:

---
methods: [POST]
auth: optional
---
#!/usr/bin/env bash
set -euo pipefail

if [ "$(request auth signed_in)" != "true" ]; then
  respond redirect "/auth/login?return_to=/contacts"
  exit 0
fi

sub="$(request auth sub)"
# … signed-in work …

Signed out, that handler returns 303 with Location: /auth/login?return_to=/contacts. The form data is not kept, so the visitor submits again after login.

Test before you publish

handler-test runs a handler against your workspace without a published mount:

handler-test pages/contacts/create.api.sh --method POST --form 'name=Test Person' --auth '{"signed_in":false}'
# status 401, body "sign in required"

handler-test pages/contacts/create.api.sh --method POST --form 'name=Test Person' --auth '{"signed_in":true}'
# status 303, Location /contacts

--auth only accepts signed_in. sub, role, and groups always come from your own session, so you cannot test as another user. Passing sub fails with --auth cannot set 'sub'. A signed-in test run really executes the handler, so it writes files in your workspace. Delete them after.

Mount policy vs handler auth

GoalUse
Public pages, signed-in forms onlyship create … --policy 644 + auth: required on those handlers
Members-only app (whole mount)--policy 640 (members) or 600 (owner only). Anonymous visitors hit /auth/login at the gate
Know who submitted on a public siteauth: required + request auth …

For 640 and 600 mounts you usually do not need client-side login redirects. The mount gate handles anonymous traffic. Keep auth: required on handlers when the site is public (644) or you want a second check. Change the policy later with ship update <id> --policy MODE.

auth valueBehavior
publicAnyone can POST (default for open forms)
optionalScript runs either way; branch on request auth signed_in
requiredUnsigned requests get 401 sign in required; the script does not run
Situationsecurityauth
Signed-in member tools on a 640 mountinvoker (default)usually required
Signed-in non-member writes on a 644 mount (forum, guestbook)definerrequired
Anonymous public form that writes your filesdefinerpublic or omit

Session claims in handlers

After login, *.api.sh can read:

FieldMeaning
signed_inWhether a publish session is present
subPlatform identity UUID
sidLogin username
name, emailFrom the login provider when available
providerLogin provider id (e.g. google, demo)
roleowner | write | read if a repository member; else omitted / null
groupsGroup names in that repository (empty if not a member)

A logged-in platform user who is not a repository member still gets a session (role null, empty groups). Role and groups are mount-scoped to the repository that shipped this site.

security: definer does not change these claims. VFS and sql run as the publisher; request auth is still the visitor. Get the current user with request auth sub (or $SAILS_AUTH_JSON .sub). Do not use Unix id or whoami.

Store only sub on records. Resolve display names at read time:

authors="$(identity get "$id1" "$id2")"
# [{ "id": "…", "name": "Ada Lovelace", "sid": "ada" }, …]

Public fields only: id, name, sid. Never email. Batch up to 64 ids. Unknown or invalid ids still come back with name and sid set to null. Names refresh when that user next signs in, and calling identity get marks the handler ETag-volatile.

For per-user rows in SQLite, sql owner enable (see SQL) makes an invoker handler see only that visitor's rows and stamps inserts with their id.

What not to do

  • Do not implement /auth/login, /auth/callback, or /auth/logout in your site. The platform serves them.
  • Do not put OAuth client secrets or session signing keys in workspace files.
  • Do not snapshot name onto records when you can store sub and call identity get.
  • Do not expect auth: required to redirect a form. It returns 401.

Failure modes

What you seeWhat it meansWhat to do
Whole site forces loginMount policy is 600 (the default) or 640Public marketing + optional login needs --policy 644 (ship update <id> --policy 644)
sign in required page after submitting a formauth: required returns 401 to signed-out visitorsAdd a Sign in link, or use auth: optional and respond redirect "/auth/login?return_to=…"
Signed-in visitor's POST fails to writeHandler is invoker and the visitor is not a repo memberAdd security: definer (owner only) and write under one dedicated path
Form accepts anonymous POSTsHandler auth is public or missingSet auth: required
role is null for a signed-in visitorThey are not a repo memberExpected on a public site. Membership is separate from login
handler-test rejects --auth '{"sub":…}'Only signed_in can be setTest as yourself, or ask a teammate to sign in on the live mount
You built custom OAuth in client JSSecrets would leak or be unverifiableUse /auth/login
whoami in the handlerThat is the VFS session, not the visitorrequest auth sub

How to: Publish a site, Put a form on a site. Feature: Publish.