Skip to content

Function: createNextRbac() ​

ts
function createNextRbac<T, G>(options): NextRbacFor<T, ScopedGuardName<T, G>>;

Defined in: packages/next/src/server.ts:243

Server-side helpers for the Next.js App Router (server components, server actions, route handlers).

ts
// lib/rbac.ts
export const { getSnapshot, can, requirePermission, withPermission, withRbac } = createNextRbac({
  rbac,
  getSubject: async () => {
    const session = await auth()
    return session?.user ? { type: 'user', id: session.user.id } : null
  },
})

With a typed engine (createRbac<AppTypes>(...)), names are checked: requirePermission('typo') is a type error. With per-guard names, the helpers take the names of the guard option (the default guard's without it).

Type Parameters ​

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

Parameters ​

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

Returns ​

NextRbacFor<T, ScopedGuardName<T, G>>

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