Skip to content

Function: createGuards() ​

ts
function createGuards<T, G>(defaults?): GuardsFor<TypesOf<T>, G>;

Defined in: packages/sveltekit/src/server/guards.ts:216

Builds the guards with shared defaults, e.g. redirects for page loads:

ts
// src/lib/server/guards.ts
export const { requirePermission, requireRole } = createGuards<AppRbac>({ unauthenticatedRedirect: '/login' })

// src/routes/admin/+page.server.ts
export const load = async (event) => {
  await requireRole(event, 'admin')
}

Pass the same type argument as createRbac<AppRbac>() (or typeof rbac) to check names at compile time. With per-guard names, G is the guard of rbacHandle() (default: the default guard): createGuards<AppRbac, 'api'>(). It is a type only; the hook's guard still decides at runtime.

Type Parameters ​

Type ParameterDefault type
T extends RbacTypeConfig | Rbac<RbacTypeConfig>DefaultRbacTypes
G extends stringDefaultGuardName<TypesOf<T>> & GuardName<TypesOf<T>>

Parameters ​

ParameterType
defaults?GuardHandlers

Returns ​

GuardsFor<TypesOf<T>, G>

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