Authentication
Use Mantis built-in email/password and OTP auth in _mantis.users, or verify external JWTs from Clerk, Auth.js, or Supabase Auth.
Mantis supports two authentication modes in mantis/schema.ts:
- Built-in Auth — Zero external providers. End-users live in
_mantis.usersinside your project's database, with PBKDF2 password hashing, 6-digit email OTP codes, revocable sessions, HS256 JWTs,client.auth, and a ReactuseUserhook. - External JWKS Auth — Keep Clerk, Auth.js, Auth0, or Supabase Auth. Mantis verifies their JWTs against your provider's public JWKS endpoint.
Both modes feed directly into your table and file access rules (rule.signedIn(),
rule.owner("userId"), rule.role("admin")).
Built-in auth
1. Enable it in mantis/schema.ts
import { defineSchema, rule, t, table } from "@shipmantis/sdk";
export default defineSchema(
{
notes: table({
userId: t.uuid(),
body: t.text(),
}).rules({
read: rule.owner("userId"),
insert: rule.owner("userId"),
update: rule.owner("userId"),
delete: rule.owner("userId"),
}),
},
{
auth: {
builtin: true,
providers: ["password", "email"],
sessionDays: 30,
},
},
);| Field | Default | What it controls |
|---|---|---|
builtin | — | Set to true to enable built-in auth. |
providers | ["password", "email"] | Allowed sign-in flows: "password" (email + password) and "email" (one-time verification codes). |
sessionDays | 30 | Session and JWT lifetime in days. |
roleClaim | "role" | JWT claim inspected by rule.role(...). |
Push the schema with npx shipmantis push. Changing auth on main once tables exist is held
for human approval.
2. Sign users up and in with client.auth
When built-in auth is enabled, client.auth automatically stores the active session token in
memory and sends it as Mantis-User-Token on every query, live query, and storage call.
// Create an account (hashes password with PBKDF2-SHA256, 100,000 iterations)
const { user, token, expiresAt } = await mantis.auth.signUp(
"ada@example.com",
"a-strong-passphrase",
{ displayName: "Ada" },
);
// Sign in on a returning visit
await mantis.auth.signIn("[email protected]", "a-strong-passphrase");
// Fetch the verified current user from _mantis.users
const current = await mantis.auth.me();
// Revoke the session in _mantis.user_sessions and clear the token
await mantis.auth.signOut();If you persist tokens across page reloads (for example in an HTTP-only cookie or sessionStorage),
restore the token on startup with mantis.auth.setToken(savedToken) and listen to changes with
mantis.auth.onAuthChange(listener).
3. React hook: useUser
"use client";
import { useUser } from "@shipmantis/sdk/react";
import { mantis } from "@/lib/mantis-browser";
export function AccountBadge() {
const { user, loading, error, refresh } = useUser(mantis);
if (loading) return <span>Loading…</span>;
if (!user) return <a href="/login">Sign in</a>;
return (
<div>
<span>{user.email}</span>
<button onClick={() => mantis.auth.signOut()}>Sign out</button>
</div>
);
}4. Inspect end-users in the dashboard
Every project has a Users tab in the dashboard listing rows from
_mantis.users (email, role, verification status, metadata, and signup timestamp). Because
_mantis.users lives inside each branch database, branches get isolated test users too.
External providers (Clerk, Auth.js, Supabase Auth)
Already using another auth provider? Pass its JWKS URL and issuer instead of builtin: true:
export default defineSchema(
{ /* tables */ },
{
auth: {
jwks: "https://example.clerk.accounts.dev/.well-known/jwks.json",
issuer: "https://example.clerk.accounts.dev",
audience: "my-app", // optional
roleClaim: "role", // optional, defaults to "role"
},
},
);Then pass a userToken callback to createClient:
import { createClient } from "@shipmantis/sdk";
import schema from "../mantis/schema";
export const mantis = createClient({
url: process.env.NEXT_PUBLIC_MANTIS_URL!,
key: process.env.NEXT_PUBLIC_MANTIS_PUBLISHABLE_KEY!,
schema,
userToken: () => window.Clerk?.session?.getToken(),
});See Access rules for row-level owner, role, signedIn, and where rules.