Function: createRbacServer()
ts
function createRbacServer<T, G, A>(options): RbacServer<T, G, A>;Defined in: packages/react-router/src/server.ts:225
Creates the server side of the integration: getRbac(args), getSnapshot(args) and the guards, for loaders and actions. Guards resolve to the subject when the check passes, and throw a Response otherwise: a 401 or 403 JSON response (the route's ErrorBoundary gets it, isRouteErrorResponse(error) is true), or a redirect.
ts
// app/rbac.server.ts
export const rbacServer = createRbacServer({
rbac,
getSubject: async ({ request }) => {
const user = await getUser(request)
return user ? { type: 'user', id: user.id } : null
},
unauthenticatedRedirect: '/login',
})
// app/routes/admin.tsx
export async function loader(args: Route.LoaderArgs) {
await rbacServer.requireRole(args, 'admin')
return { stats: await getStats() }
}Type Parameters
| Type Parameter | Default type |
|---|---|
T extends RbacTypeConfig | DefaultRbacTypes |
G extends string | DefaultGuardName<T> & GuardName<T> |
A extends RouteArgs | RouteArgs |
Parameters
| Parameter | Type |
|---|---|
options | RbacServerOptions<T, G, A> & (T extends object ? GuardOptions<CheckGuard<T, G>> : unknown) |
Returns
RbacServer<T, G, A>
