Function: createRbacContext()
ts
function createRbacContext<I, T, G>(options): (input) => Promise<RbacContext<T, G>>;Defined in: packages/trpc/src/context.ts:84
Builds the rbac part of your tRPC context: returns a function that resolves the subject from its argument and gives { rbac } (a SubjectRbac, or null for guests). Spread it into what createContext returns.
ts
const rbacContext = createRbacContext({
rbac,
getSubject: (user: User | null) => (user ? { type: 'user', id: user.id } : null),
})
export async function createContext({ req }: FetchCreateContextFnOptions) {
const user = await getUser(req)
return { user, ...(await rbacContext(user)) }
}With a typed engine (createRbac<AppRbac>()), ctx.rbac is typed with its names. With per-guard names, the guard option picks the names of ctx.rbac.
Type Parameters
| Type Parameter | Default type |
|---|---|
I | unknown |
T extends RbacTypeConfig | DefaultRbacTypes |
G extends string | DefaultGuardName<T> |
Parameters
| Parameter | Type |
|---|---|
options | RbacContextOptions<I, T, G> & (T extends object ? GuardOptions<CheckGuard<T, G>> : unknown) |
Returns
(input) => Promise<RbacContext<T, G>>
