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
| Goal | Use |
|---|---|
| Public pages, signed-in forms only | ship 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 site | auth: 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 value | Behavior |
|---|---|
public | Anyone can POST (default for open forms) |
optional | Script runs either way; branch on request auth signed_in |
required | Unsigned requests get 401 sign in required; the script does not run |
| Situation | security | auth |
|---|---|---|
Signed-in member tools on a 640 mount | invoker (default) | usually required |
Signed-in non-member writes on a 644 mount (forum, guestbook) | definer | required |
| Anonymous public form that writes your files | definer | public or omit |
Session claims in handlers
After login, *.api.sh can read:
| Field | Meaning |
|---|---|
signed_in | Whether a publish session is present |
sub | Platform identity UUID |
sid | Login username |
name, email | From the login provider when available |
provider | Login provider id (e.g. google, demo) |
role | owner | write | read if a repository member; else omitted / null |
groups | Group 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/logoutin your site. The platform serves them. - Do not put OAuth client secrets or session signing keys in workspace files.
- Do not snapshot
nameonto records when you can storesuband callidentity get. - Do not expect
auth: requiredto redirect a form. It returns401.
Failure modes
| What you see | What it means | What to do |
|---|---|---|
| Whole site forces login | Mount policy is 600 (the default) or 640 | Public marketing + optional login needs --policy 644 (ship update <id> --policy 644) |
sign in required page after submitting a form | auth: required returns 401 to signed-out visitors | Add a Sign in link, or use auth: optional and respond redirect "/auth/login?return_to=…" |
| Signed-in visitor's POST fails to write | Handler is invoker and the visitor is not a repo member | Add security: definer (owner only) and write under one dedicated path |
| Form accepts anonymous POSTs | Handler auth is public or missing | Set auth: required |
role is null for a signed-in visitor | They are not a repo member | Expected on a public site. Membership is separate from login |
handler-test rejects --auth '{"sub":…}' | Only signed_in can be set | Test as yourself, or ask a teammate to sign in on the live mount |
| You built custom OAuth in client JS | Secrets would leak or be unverifiable | Use /auth/login |
whoami in the handler | That is the VFS session, not the visitor | request auth sub |
How to: Publish a site, Put a form on a site. Feature: Publish.