Skip to content

Function: createRbacServer() ​

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

Defined in: packages/astro/src/server.ts:231

Creates the server side of the integration: getRbac(context), getSnapshot(context) and the guards, for pages, endpoints and middleware. A guard resolves to undefined when the check passes, and to a Response to return otherwise: a 401 or 403 JSON response, or a redirect.

ts
// src/rbac.ts
export const rbacServer = createRbacServer({
  rbac,
  getSubject: ({ locals }) => (locals.user ? { type: 'user', id: locals.user.id } : null),
  unauthenticatedRedirect: '/login',
})
astro
---
// src/pages/admin.astro
const denied = await rbacServer.requireRole(Astro, 'admin')
if (denied) return denied
---

Type Parameters ​

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

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.