Skip to content

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 ParameterDefault type
T extends RbacTypeConfigDefaultRbacTypes
G extends stringDefaultGuardName<T> & GuardName<T>
A extends RouteArgsRouteArgs

Parameters ​

ParameterType
optionsRbacServerOptions<T, G, A> & (T extends object ? GuardOptions<CheckGuard<T, G>> : unknown)

Returns ​

RbacServer<T, G, A>

Released under the MIT License. Inspired by spatie/laravel-permission.