Skip to content

Function: createRbacServer() ​

ts
function createRbacServer<T, G>(options): RbacServer<T, G>;

Defined in: packages/tanstack-start/src/server.ts:205

Creates the server side of the integration: getRbac(request), getSnapshot(request) and the guards, for server functions, middleware and server routes. Guards resolve to the subject when the check passes, and throw a Response otherwise: a 401 or 403 JSON response, or what onUnauthenticated / onForbidden return.

ts
// src/rbac.ts
export const rbacServer = createRbacServer({
  rbac,
  getSubject: async (request) => {
    const user = await getUser(request)
    return user ? { type: 'user', id: user.id } : null
  },
})

// a server function
export const getStats = createServerFn().handler(async () => {
  await rbacServer.requireRole(getRequest(), 'admin')
  return loadStats()
})

Type Parameters ​

Type ParameterDefault type
T extends RbacTypeConfigDefaultRbacTypes
G extends stringDefaultGuardName<T> & GuardName<T>

Parameters ​

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

Returns ​

RbacServer<T, G>

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