Access rules
Decide what the publishable key may read and write, per table and per signed-in user.
The secret key can do anything, so it stays on your server. The publishable key is safe in a browser because it can do nothing until a rule allows it. Rules sit on the table in the schema.
import { defineSchema, rule, t, table } from "@shipmantis/sdk";
export default defineSchema(
{
posts: table({
title: t.text(),
published: t.boolean().default(false),
authorId: t.text(),
}).rules({
read: rule.any(rule.where({ published: true }), rule.owner("authorId")),
insert: rule.owner("authorId"),
update: rule.owner("authorId"),
delete: rule.all(rule.owner("authorId"), rule.role("admin")),
}),
},
{
auth: {
jwks: "https://example.clerk.accounts.dev/.well-known/jwks.json",
issuer: "https://example.clerk.accounts.dev",
},
},
);Your PHP code holds the secret key, which skips every rule: on the server, your policies and gates decide what a request may do. Rules are for the browser: the publishable key in a page's script, reading and writing the Mantis schema's tables directly, with live queries or uploads straight to storage. It can do nothing until a rule allows it.
Rules sit on the table in database/mantis.php, pushed with php artisan mantis:push:
<?php
return [
'tables' => [
'posts' => [
'columns' => [
'title' => ['type' => 'text'],
'published' => ['type' => 'boolean', 'default' => false],
'authorId' => ['type' => 'text'],
],
'rules' => [
'read' => ['any' => [
['where' => ['published' => true]],
['owner' => 'authorId'],
]],
'insert' => ['owner' => 'authorId'],
'update' => ['owner' => 'authorId'],
'delete' => ['all' => [['owner' => 'authorId'], ['role' => 'admin']]],
],
],
],
'auth' => [
'jwks' => 'https://example.clerk.accounts.dev/.well-known/jwks.json',
'issuer' => 'https://example.clerk.accounts.dev',
],
];Tables your Laravel migrations made have no rules: the publishable key can't reach them at all.
Rules
| Rule | Allows |
|---|---|
rule.public() | Anyone with the publishable key. |
rule.signedIn() | Any signed-in user. |
rule.owner("column") | Rows whose column holds the signed-in user's id. The column is text or uuid. |
rule.role("admin") | Users with that role. |
rule.where({ column: value }) | Rows whose columns hold those values. |
rule.any(...), rule.all(...) | Any one of the rules, or all of them. |
- Deny by default. An operation with no rule is refused for the publishable key.
- Rows, not tables.
ownerandwherenarrow a read, an update or a delete to the rows they allow; the rest are left alone, as if they weren't there. - Owners fill themselves in. On an insert, an
ownercolumn takes the user's id from the token, whatever the browser sent. An update can't change a column the update rule depends on. - The secret key skips the rules. So does the agent, which holds it.
| Rule | Allows |
|---|---|
['public' => true] | Anyone with the publishable key. |
['signedIn' => true] | Any signed-in user. |
['owner' => 'column'] | Rows whose column holds the signed-in user's id. The column is text or uuid. |
['role' => 'admin'] | Users with that role. |
['where' => ['column' => $value]] | Rows whose columns hold those values. |
['any' => [...]], ['all' => [...]] | Any one of the rules, or all of them. |
- Deny by default. An operation with no rule is refused for the publishable key.
- Rows, not tables.
ownerandwherenarrow a read, an update or a delete to the rows they allow; the rest are left alone, as if they weren't there. - Owners fill themselves in. On an insert, an
ownercolumn takes the user's id from the token, whatever the browser sent. An update can't change a column the update rule depends on. - The secret key skips the rules. So do your PHP code and the agent, which hold it.
Signed-in users
Mantis works with both built-in authentication (auth: { builtin: true }, which signs
HS256 JWTs for users stored in _mantis.users) and external JWT providers like Clerk, Auth.js,
Auth0, or Supabase Auth (verified against their public JWKS).
For external providers, configure auth with your provider's JWKS:
auth field | What it holds |
|---|---|
jwks | The provider's JWKS URL, over https://. |
issuer | The iss the tokens must carry. |
audience | Optional. The aud the tokens must carry. |
roleClaim | The claim that holds the user's role, or a list of roles. Default: role. |
A token's sub is the user's id for owner. With a list of roles, rule.role matches any of
them. An expired token, or one from another issuer, is refused with 401.
Give the client a function that returns the current token. It is called before every request, so it can hand back a fresh one:
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(),
});init writes MANTIS_URL and MANTIS_PUBLISHABLE_KEY to .env.local. A browser bundle only
sees what your framework exposes to it, so in Next.js copy them to NEXT_PUBLIC_ names. Never
expose the secret key that way.
The token goes in the mantis-user-token header. Without one the caller is signed out, so only
public and where rules can allow it.
Mantis works with both built-in authentication and external JWT providers like Clerk, Auth0, or any provider that signs JWTs and publishes its keys as a JWKS.
auth key | What it holds |
|---|---|
jwks | The provider's JWKS URL, over https://. |
issuer | The iss the tokens must carry. |
audience | Optional. The aud the tokens must carry. |
roleClaim | The claim that holds the user's role, or a list of roles. Default: role. |
A token's sub is the user's id for owner. With a list of roles, role matches any of them.
An expired token, or one from another issuer, is refused with 401.
Laravel's own sessions aren't JWTs, so a user signed in with Breeze, Jetstream or Fortify is
signed out as far as Mantis can tell: only public and where rules let them in. For rules about
the user, sign in with a JWT provider, or keep that table on the server and serve it from your
controllers.
The page's script gets the publishable key from your config, never from .env as a whole:
'mantis' => [
'url' => env('MANTIS_URL'),
'publishable_key' => env('MANTIS_PUBLISHABLE_KEY'),
],<meta name="mantis-url" content="{{ config('services.mantis.url') }}">
<meta name="mantis-key" content="{{ config('services.mantis.publishable_key') }}">
@vite('resources/js/mantis.js')import { createClient } from "@shipmantis/sdk";
const meta = (name) => document.querySelector(`meta[name="${name}"]`).content;
export const mantis = createClient({
url: meta("mantis-url"),
key: meta("mantis-key"),
userToken: () => window.Clerk?.session?.getToken(),
});npm install @shipmantis/sdk once. The token goes in the mantis-user-token header; without one
the caller is signed out.
Changing rules
A rule change is a step in the plan like any other. Making a rule stricter applies at once. A rule
that lets more people in is held on main until a person approves it, because it can expose rows
that were private. So is a change to auth once the project has tables.
Examples
Private notes, one user each:
notes: table({ userId: t.text(), body: t.text() }).rules({
read: rule.owner("userId"),
insert: rule.owner("userId"),
update: rule.owner("userId"),
delete: rule.owner("userId"),
}),The browser can insert without sending userId; it is filled from the token.
A public catalogue only admins edit:
products: table({ name: t.text(), price: t.number() }).rules({
read: rule.public(),
insert: rule.role("admin"),
update: rule.role("admin"),
delete: rule.role("admin"),
}),With Clerk, put the role in the session token's claims ({ "role": "{{user.public_metadata.role}}" })
or set roleClaim to the claim your provider uses.
Published posts for everyone, drafts for their author is the schema at the top of this page:
rule.any(rule.where({ published: true }), rule.owner("authorId")).
Rows a whole team shares. Rules see the user, not their team, so a team-wide table is read
on your server with the secret key, after your code checks the user's membership. Give the
browser only owner and public reads.
Private notes, one user each:
'notes' => [
'columns' => ['userId' => ['type' => 'text'], 'body' => ['type' => 'text']],
'rules' => [
'read' => ['owner' => 'userId'],
'insert' => ['owner' => 'userId'],
'update' => ['owner' => 'userId'],
'delete' => ['owner' => 'userId'],
],
],The browser can insert without sending userId; it is filled from the token.
A public catalogue only admins edit. Your Laravel admin edits it on the server, with the secret key; the browser only reads:
'products' => [
'columns' => ['name' => ['type' => 'text'], 'price' => ['type' => 'number']],
'rules' => ['read' => ['public' => true]],
],Mantis::table('products')->update(['price' => 19.0], ['id' => $product]); // from an admin controllerPublished posts for everyone, drafts for their author is the schema at the top of this page.
Rows a whole team shares. Rules see the user, not their team, so serve a team-wide table from a
controller after your policy checks the membership, and give the browser only owner and
public reads.
Edge cases
| Case | What happens |
|---|---|
| No rule for the operation | 403 for the publishable key. |
| A read whose rule matches some rows | Only those rows come back; the others act as if they weren't there. An update or delete leaves them alone. |
An insert that names another user in the owner column | The column takes the signed-in user's id anyway. |
An update that changes the owner column, or a where column the rule depends on | Refused with 403. |
A user token on a schema with no auth | 401: the project takes no user tokens. |
| An expired token, or one from another issuer or audience | 401. With userToken as a function, the client asks for a fresh one on the next request. |
| A signed-out visitor | Only public and where rules can let them in. |
| A role claim holding a list | rule.role("admin") matches when any of them is admin. |
| Live queries | Use the same rules, row by row; see Live queries. |
| The secret key, the agent, the dashboard | Skip the rules. |