Authentication
elements man recipes/authenticationForm-based authentication: two pages, /signin and /signup, sharing one
schema and one set of RPC handlers. Credentials store as bcrypt hashes in
Postgres. Each RPC validates against the hash and calls session.login(). The
reactive session updates both pages immediately on success, no full-page reload.
The Elements schema setup automatically installs the pgcrypto extension, so
crypt() and genSalt() are available in migrations and rpc.
Migration
Terminalelements create migration 'add users' -tables=users
app/migrations/<timestamp>-add-users.migration.sql-- add users -- Auto-update updatedAt on row changes. create or replace function touchUpdatedAt() returns trigger language plpgsql as $$ begin new.updatedAt = now(); return new; end; $$; create table users ( id uuid primary key default uuidGenerateV7(), createdAt timestamptz not null default now(), updatedAt timestamptz not null default now(), email text not null unique, passwordHash text not null ); create trigger usersTouchUpdatedAt before update on users for each row execute function touchUpdatedAt();
The two columns added to the scaffold's table are email (unique, so no two
users share one) and passwordHash (stores the bcrypt hash, never the
plaintext). Email is the identifier because it is what a password manager
keys on and what a reset flow needs; a handle column can be added beside it
as a display name.
Store the email lowercased. Postgres text is case-sensitive, so the unique
constraint treats Ada@x.com and ada@x.com as two values, though they are the
same mailbox. The RPC functions below lowercase the email before they write it
or look it up.
Shared auth RPC
Two pages call these, so they go in app/shared/services/. A page's @rpc
functions normally live in its own template.ehtml (elements man html/templates); the second consumer is what moves them out.
app/shared/services/auth.tsimport { sql, session, AuthError } from "@elements/app"; interface User { id: string; email: string; } export const MIN_PASSWORD = 8; /** * Both RPC take the raw form values, so both validate. A browser check is a * convenience the caller can skip; this is the check that counts. */ function normalizeEmail(email: string): string { return email.trim().toLowerCase(); } function isEmail(email: string): boolean { return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email); } /** @rpc */ export function signin(email: string, password: string) { let address = normalizeEmail(email); if (!address || !password) { throw new AuthError("enter your email and password"); } let user = sql<User>(` select id, email from users where email = ${address} and passwordHash = crypt(${password}, passwordHash) `).firstOrThrow(new AuthError("invalid email or password")); session.login({ userId: user.id, userName: user.email, }); } /** @rpc */ export function signup(email: string, password: string) { let address = normalizeEmail(email); if (!isEmail(address)) { throw new AuthError("enter a valid email address"); } if (password.length < MIN_PASSWORD) { throw new AuthError(`password must be at least ${MIN_PASSWORD} characters`); } let taken = !sql(` select 1 from users where email = ${address} `).empty(); if (taken) { throw new AuthError("that email is already registered"); } let user = sql<{ id: string }>(` insert into users ( email, passwordHash ) values ( ${address}, crypt(${password}, genSalt('bf', 12)) ) returning id `).firstOrThrow(); session.login({ userId: user.id, userName: address, }); } /** @rpc */ export function signout() { session.logout(); }
Signin says "invalid email or password", never which one was wrong. Telling a stranger that an address exists but the password was wrong turns the form into a way to enumerate your users. Signup has to say "already registered" to be usable, which leaks the same fact; a product that cannot afford that sends a "someone tried to register this address" email instead and shows the same "check your inbox" screen either way.
The unique index is the guarantee; the taken check is the friendly
message. When two signups race past select 1, the unique index accepts one
and rejects the other. Catch SqlError around the insert and rethrow
AuthError("that email is already registered") so both paths show the same
message.
The signin query reads passwordHash = crypt(${password}, passwordHash). Both
occurrences are the same column. crypt() re-hashes the submitted password
using the algorithm and salt encoded in the stored hash and compares the result,
all in one expression. The plaintext never leaves Postgres and TypeScript never
sees the stored hash.
Signup uses genSalt('bf', 12) to generate a new bcrypt salt for the new user.
bf is bcrypt; pick a different scheme only if you understand the trade-off.
Pass the cost. The second argument to genSalt is the bcrypt work factor.
A higher work factor makes one hash take longer, so each password guess takes
longer for someone holding a stolen users table. pgcrypto defaults bf to 6,
which is far too cheap on modern hardware. Measured on the bundled Postgres:
Timingscrypt('x', gen_salt('bf')) 4.1 ms cost 6, the default crypt('x', gen_salt('bf', 12)) 235 ms cost 12
That is 57x more work per guess. Cost 12 is the right default today. Raise it as hardware gets faster; the cost is stored in the hash, so old rows keep verifying at the cost they were written with and you can re-hash on next signin.
signout has no caller in these two pages, which redirect a signed-in visitor
away. Call it from a template a signed-in user sees, such as a header or an account
menu, as onclick={() => signout()}.
AuthError is a safe error: the server sends its message to the browser
unchanged, and the RPC call in the browser throws it.
Declaring session fields
session.login({ userId, userName }), session.get('userId'), and
session.getOrThrow('userName') only typecheck once you declare those fields on
SessionData. Do it once per app in app/types/session.d.ts. The scaffold
includes this file with the block commented out:
app/types/session.d.tsdeclare module "@elements/app" { interface SessionData { userId: string; userName: string; } }
SessionData is a global augmentation: declare it once and every
session.login/get/getOrThrow across the app is typed against it. Without
it, keyof SessionData is empty, so session.login({ userId }) reports
"expected 0 arguments" and session.get('userId') rejects the key.
Signin page
Terminalelements create page signin
Update app/pages/signin/index.ts and template.ehtml with the contents below.
app/pages/signin/index.tsimport { Request, Response, redirect, session } from "@elements/app"; import signin from "#app/pages/signin/template"; export default function route(req: Request, res: Response) { if (session.isLoggedIn()) { redirect("/"); return; } return new signin(); }
app/pages/signin/template.ehtmlimport "./style.css"; import { redirect } from "@elements/app"; import { Alert } from "@elements/ui/alert"; import { Button } from "@elements/ui/button"; import { Field } from "@elements/ui/field"; import { Input } from "@elements/ui/input"; import { PasswordInput } from "@elements/ui/password-input"; import { signin } from "#app/shared/services/auth"; interface SigninForm { email: string; password: string; error: string; } function onSubmit(form: SigninForm) { try { signin(form.email, form.password); form.error = ""; redirect("/"); } catch (err: any) { form.error = err.message; } } <html class="page-signin" ( private form: SigninForm = { email: "", password: "", error: "", }, )> <main class="page-shell"> <form class="stack" onsubmit={() => onSubmit(form)}> <h1>sign in</h1> <Alert e:if={form.error} variant="danger" description={form.error} /> <Field label="email" required> <Input type="email" name="email" value={form.email} placeholder="ada@example.com" attrs={{ autocomplete: "email", autocapitalize: "none", spellcheck: "false" }} /> </Field> <Field label="password" required> <PasswordInput name="password" value={form.password} autocomplete="current-password" /> </Field> <Button type="submit" variant="primary">sign in</Button> <a href="/signup">need an account? sign up</a> </form> </main> </html>
The form's controls are @elements/ui templates, which every new app has
(elements man ui). A Field (opens in a new tab) around each control
renders the label, ties it to the control, and marks it required.
Input (opens in a new tab) and PasswordInput (opens in a new tab)
bind the two values two-way; the password field gets a show-and-hide toggle.
The error line is an Alert (opens in a new tab), announced as soon as it
appears, and the submit is a Button (opens in a new tab).
Signup page
Terminalelements create page signup
Update app/pages/signup/index.ts and template.ehtml.
app/pages/signup/index.tsimport { Request, Response, redirect, session } from "@elements/app"; import signup from "#app/pages/signup/template"; export default function route(req: Request, res: Response) { if (session.isLoggedIn()) { redirect("/"); return; } return new signup(); }
app/pages/signup/template.ehtmlimport "./style.css"; import { redirect } from "@elements/app"; import { Alert } from "@elements/ui/alert"; import { Button } from "@elements/ui/button"; import { Field } from "@elements/ui/field"; import { Input } from "@elements/ui/input"; import { PasswordInput } from "@elements/ui/password-input"; import { signup, MIN_PASSWORD } from "#app/shared/services/auth"; interface SignupForm { email: string; password: string; error: string; } function onSubmit(form: SignupForm) { try { signup(form.email, form.password); form.error = ""; redirect("/"); } catch (err: any) { form.error = err.message; } } <html class="page-signup" ( private form: SignupForm = { email: "", password: "", error: "", }, )> <main class="page-shell"> <form class="stack" onsubmit={() => onSubmit(form)}> <h1>sign up</h1> <Alert e:if={form.error} variant="danger" description={form.error} /> <Field label="email" required> <Input type="email" name="email" value={form.email} placeholder="ada@example.com" attrs={{ autocomplete: "email", autocapitalize: "none", spellcheck: "false" }} /> </Field> <Field label="password" description={`at least ${MIN_PASSWORD} characters`} required> <PasswordInput name="password" value={form.password} autocomplete="new-password" attrs={{ minlength: MIN_PASSWORD }} /> </Field> <Button type="submit" variant="primary">create account</Button> <a href="/signin">have an account? sign in</a> </form> </main> </html>
The password Field shows the minimum length as its description, on the
reserved message line under the control.
Input attributes the browser needs
A password manager decides what to offer from the attributes on the form's inputs. The attributes below make it offer to fill the saved password on signin and offer to generate a password on signup.
| attribute | signin | signup | why |
|---|---|---|---|
type |
email |
email |
phone keyboards show the @ key, and the browser validates the shape |
autocomplete on email |
email |
email |
identifies the field to the manager and to autofill |
autocomplete on password |
current-password |
new-password |
current-password offers the saved one; new-password is what makes the browser suggest a strong password and offer to save it |
name and id |
yes | yes | a manager will not reliably fill an unnamed input; name is on the control, and Field assigns the id and ties its label to it |
autocapitalize: "none" |
yes | yes | iOS capitalizes the first letter of a text field, which corrupts the address |
spellcheck: "false" |
yes | yes | stops the red underline under every address |
minlength |
no | MIN_PASSWORD |
the browser blocks submit before a round trip; the RPC still checks |
type, name, placeholder and value are attributes of Input;
autocomplete, autocapitalize and spellcheck go in its attrs, which puts
any other attribute on the native input. PasswordInput takes autocomplete
as its own attribute and minlength in attrs. required is an attribute of
the Field, which sets it on the control and draws the marker on the label.
autocomplete="new-password" is the one to get right. It is the only
signal that asks the browser to generate a password, and it is easy to write
current-password on both pages, or to omit it and get neither behavior. The
two pages use different values on purpose.
With type="email" the browser checks the address format before it submits the
form, with no code. Keep isEmail in the RPC as well: a client can call the RPC
without using the form.
One form object for signin and signup
One form object, not a field per attribute. email, password and
error are one SigninForm, declared private and passed to the handler
whole. A handler mutating form.error is live; reassigning a bare error
attribute from inside a handler is not (elements man html/reactivity), so the
object is what makes the error line work at all. It also keeps the constructor
to one attribute as the form grows a field.
The handler is a named function, not markup. onSubmit(form) holds the
try/catch, the error assignment and the redirect; the <form> tag
has only onsubmit={() => onSubmit(form)}. A named function can be tested and
reused; code written inline in an attribute cannot.
A signed-in visitor is turned away at the route, not in the page. The route
redirects before the page renders. Branching on session.isLoggedIn() inside
the template instead shows the signed-in markup for a moment, after
session.login() succeeds and before redirect() loads the next page; see
server rendering (opens in a new tab). Either page still works as a deep link.
Routes
Register the pages in index.ts alongside the scaffold's existing structure:
index.tsimport signin from "#app/pages/signin"; import signup from "#app/pages/signup"; // ... app.route("/signin", signin); app.route("/signup", signup);
Notes
session.login({ userId, userName }).userIdis required and marks the session as logged in.userNameis the display name, the email here. Add ahandlecolumn and put that inuserNamewhen you want a friendlier name.crypt()andgenSalt()come from thepgcryptoextension. Elements installs the extension at project setup, so they're always available.- The unique constraint on
emailguards against a duplicate. The RPC checks first for the friendly message; on a race the constraint rejects the second insert withSqlError, so catch it around the insert and rethrowAuthErrorto show the same message on both paths. - Session expiry is an app-wide policy set by
session.expiresinconfig.jsoc(the scaffold sets'30d'). It applies to every session;session.login()takes only the session data (userId,userName, and any fields you declare onSessionData).