Authorization
Policies control who may run a function, and record rules control what they may do.
Authorization checks the scope before the handler runs and checks record access inside the handler.
const const member: (scope: Scope) => scope is Membermember = (scope: Scopescope: type Scope = Member | {
actor: null;
}
Scope): scope: Scopescope is type Member = {
actor: {
id: string;
};
}
Member => scope: Scopescope.actor: {
id: string;
} | null
actor !== null;
/** Read the current member's ID. */
export const const memberId: Fn<"query", undefined, string, true, {
scope: Scope;
}, undefined, undefined>
Read the current member's ID.memberId = const query: <NoInput, undefined, string, (scope: Scope) => scope is Member, boolean, undefined>(def: Def<{
scope: Scope;
}, "query", NoInput, undefined, string, (scope: Scope) => scope is Member, boolean, undefined>) => Fn<"query", undefined, string, true, {
scope: Scope;
}, undefined, undefined>
Reads data. Can run other queries. No writes, no events, no services.query({
authorize?: (((scope: Scope) => scope is Member) & ((scope: Scope) => boolean)) | undefinedWho may call it, on every call. A type guard narrows `ctx.scope` in `handler`. Required to publish it through an
adapter: callers outside your server are untrusted. Leave it out when only your own code runs it.authorize: const member: (scope: Scope) => scope is Membermember,
async handler: (ctx: QueryCtx<{
scope: Scope;
}, Member>, input: undefined) => Promise<string>
handler(ctx: QueryCtx<{
scope: Scope;
}, Member>
ctx) {
return ctx: QueryCtx<{
scope: Scope;
}, Member>
ctx.BaseCtx<{ scope: Scope; }, Member>.scope: Memberscope.actor: {
id: string;
}
actor.id: stringid;
},
});
Policies
- Timing. The policy runs on every call, including nested calls, before input validation or a new transaction.
- Access (1). Return exactly
trueto allow the call to proceed. A type guard narrowsctx.scopein the handler. - Denial (2). Every other return value gives
forbidden, including a Promise. Policies are synchronous and read only the scope.
Publishing
Set authorize on every published function, including functions reachable from routes or Server Actions. AI and oRPC check it at build time and runtime. To allow anyone, use a policy that returns true:
export const anyone = () => true;
Set an output schema.
Internal functions
Keep internal functions off the public API. Their policies still apply. For signed-out webhooks and authentication callbacks, see Callbacks.
Record rules (can*)
Load through a helper that filters by ctx.scope.accountId. For a missing or out-of-account record, throw ctx.error.notFound(). Apply the same account filter to writes.
const booking = await findBooking(ctx, input.bookingId);
if (!canCancel(ctx.scope.actor, booking)) throw ctx.error.deny();
Make canCancel a synchronous predicate. Use it in list queries for UI flags such as can.cancel, and check it again in the mutation.
If a rule must hold at commit, put it in the write’s WHERE clause or re-select the row FOR UPDATE and check again.