Skip to content

Accounts

Roles and permissions

Every account has one app role, and routes check permissions rather than roles. A permission comes from the account's role or from a plan its organization holds. You work here when you add a staff tool, a paid feature or a new role. The whole model is the access block in starter.config.ts(Licensees only).

Roles inside an organization (owner, admin, member) are a separate system, covered in Organizations.

How it works

access: {
  permissions: ['users:read', 'users:manage', 'blog:author', 'insights:read'],

  defaultRole: 'user',

  firstAccountRole: 'superadmin',

  rolePermissions: {
    user: [],
    staff: ['users:read', 'blog:author'],
    superadmin: ['users:read', 'users:manage', 'blog:author'],
  },

  planPermissions: {
    free: [],
    pro: ['insights:read'],
    promax: ['insights:read'],
    lifetime: ['insights:read'],
  },
},
  1. permissions lists every permission. The Permission and UserRole types come from this block, so a grant or a check naming anything else fails typecheck. A check may still name users:read, users:manage or blog:author after you drop it, because the kit's own routes check those three.
  2. rolePermissions is what each role holds. planPermissions is what each plan sells, with free for an organization that holds no grant.
  3. requirePermission checks the role first. If the role doesn't grant it and a plan sells it, it reads the live grants of the organization you name.
  4. users.role is a text column. Each request that checks the session reads it again, and a value the config doesn't declare reads as defaultRole.

The two sources add up. A superadmin keeps staff powers on the free tier, and staff status never opens a paid feature: to let superadmin read insights, list insights:read under it.

PermissionOpens
users:read/dashboard/customers, the organization directory, account and billing details there
users:manageImpersonating, suspending, editing accounts, support mails, clearing two-factor authentication, granting plans, refunds
blog:authorLets the account at the blog's author address appear as the byline
insights:readThe example paid feature, sold on every paid plan

You may drop users:read, users:manage or blog:author from permissions and from the roles. Nothing then grants it, so what it opens stays closed. Dropping users:manage needs firstAccountRole: false.

Check a permission on the server

requirePermission(event, permission, orgId?) lives in layers/kit/server/utils/permissions.ts(Licensees only) and returns the session:

export default defineEventHandler(async (event) => {
  const session = await requirePermission(event, 'users:read')

  return { viewer: session.user.email }
})

It answers 401 without a verified session. When nothing grants the permission, it answers 402 with data.reason: 'upgrade-required' if some plan sells it and 403 if none does, so the client knows whether an upgrade helps.

A permission a plan sells is held by an organization, so name it. With organizations on, leaving the id out of a check that reaches the plans answers 500:

const scope = await requireOrgScope(event, getRouterParam(event, 'org') ?? '')

await requirePermission(event, 'insights:read', scope.rootOrgId)

With payments off, every permission a plan sells is granted to every signed-in account. Payments has the paid side, including requireEntitlement and the plan gate.

Check a permission in the browser

usePermissions(org?) returns role, can(permission) and pending:

<script setup lang="ts">
const route = useRoute()
const { can, pending } = usePermissions(() => String(route.params.org ?? ''))
</script>

<template>
  <InsightsPanel v-if="can('insights:read')" />
  <UpgradeCard v-else-if="!pending" />
</template>
  • Pass the organization's public id when the permission may come from a plan. With organizations on and none named, plans count for nothing and only the role answers.
  • pending is true while the plans load. Wait for it before showing an upsell, or paying customers see one flash on every load.
  • It subscribes to a query, so call it in component setup. Route middleware calls can(permission, { role }) from #layers/kit/shared/utils/permissions instead, as layers/kit/app/middleware/admin.ts(Licensees only) does to keep accounts without users:read off the staff pages.

What the browser decides is for rendering. The route's requirePermission is the check that counts.

Add a permission

List it in permissions, grant it from a role or a plan, and check it:

access: {
  permissions: [
    'users:read',
    'users:manage',
    'blog:author',
    'insights:read',
    'reports:export',
  ],

  rolePermissions: {
    user: [],
    staff: ['users:read', 'blog:author', 'reports:export'],
    superadmin: [
      'users:read',
      'users:manage',
      'blog:author',
      'reports:export',
    ],
  },
},
await requirePermission(event, 'reports:export')

A permission may be listed with no grant yet. A route gated on it then answers 403 until a role or plan grants it.

Add a role

Add a key to rolePermissions and a label as dashboard.customers.roles.<id> in your catalogue of every locale, i18n/locales/<file>. layers/kit/test/unit/role-copy.test.ts(Licensees only) fails on a missing label. Routes check permissions, never role names, so no other file needs the new role.

Staff holding users:manage assign roles from the Edit action on /dashboard/customers. That dialog never offers a role that grants users:manage, and the route refuses one with 403 role-manages-users, so a new superadmin is made by hand (Promote an account by hand).

A second staff role stays within its own permissions. The dialog offers only roles whose permissions the editor's role holds too, and the route refuses another with 403 role-exceeds-actor. With support holding users:manage and finance holding a billing permission of yours, support can neither make an account finance nor act on a finance account: editing, impersonating, suspending, the support actions, granting or taking back a plan and refunding all refuse it (Staff tools). For a staff tool of your own, assertWithinStaffReach(staffRole, targetRole), auto-imported on the server, is that rule: it throws 403 for an account holding users:manage and 403 target-exceeds-actor for one whose role holds a permission the staff role lacks. roleExceeds(role, actorRole), auto-imported in both bundles, answers whether a role holds a permission another lacks.

access.defaultRole is written explicitly on signup, so changing it needs no migration. The column's own SQL default of 'user' only covers rows written by hand or by the seed.

Staff tools

Staff holding users:manage get these actions on /dashboard/customers:

ActionRoute
Edit name, email, rolePATCH /api/admin/users/:id
SuspendPOST /api/admin/users/ban
ImpersonatePOST /api/admin/impersonate, POST /api/admin/impersonate/stop
Mail a password resetPOST /api/admin/users/password-reset
Mail a verification linkPOST /api/admin/users/verification-email
Clear sign-in lockoutsPOST /api/admin/users/unlock
Turn off two-factor authenticationPOST /api/admin/users/clear-second-factor
Grant or take back a planPOST /api/admin/billing/grant, /revoke
Refund a purchasePOST /api/admin/billing/refund

Each refuses the caller's own account with 400, and an account whose role grants users:manage with 403, so one administrator can't act on another. Each also refuses, with 403 target-exceeds-actor, an account whose role holds a permission the caller's role lacks, and the customers page offers no action on such an account, as on another administrator's. Granting a plan and refunding a purchase refuse the same way for an organization you or another administrator belong to: for a refund, the organization the payment ledger recorded for the purchase, else the one its grant sits on, else the buyer's own. The password reset, verification mail, unlock and two-factor clear each allow a staff member 30 uses an hour, and staff never see a mailed link.

Turning off two-factor authentication asks the staff member for a recent proof of their own: without a sign-in or re-authentication on the same device in the last 10 minutes it answers 403 proof-required, and the customers page asks for their password or a passkey, then carries on (Auth). It deletes the account's TOTP credential and recovery codes whether or not auth.totp is on, and its passkeys only under passkey: 'second-factor'. It signs the account out on every device, records admin.secondFactorCleared in the transaction that clears, and mails the owner in the site's default language. It works on a suspended account, so you can suspend it, clear the factor and lift the suspension. It answers 404 second-factor-off on a build that asks for no second factor, 409 no-second-factor on an account that owes none and 409 deletion-scheduled on one scheduled for deletion, and the customers page offers it only on an account that has one.

An impersonating session is read-only. layers/kit/server/middleware/impersonation.ts(Licensees only) answers every API write, anything but GET, HEAD and OPTIONS, with 403 impersonating. Stopping the impersonation and signing out still work.

An impersonation lasts at most 30 minutes, and both accounts' activity records it. admin.impersonationStarted is written before the session changes, with the moment it expires as metadata.expiresAt, and a start that cannot be recorded answers 500 and does not happen. Stop writes admin.impersonationStopped. An impersonation that ends any other way writes it too, once, with metadata.reason: expired when its time ran out, signed-out, or revoked when the account was suspended, scheduled for deletion or signed out everywhere, or the admin's device was signed out. An impersonation nobody comes back to records no end, so read it off the start's expiresAt.

The first superadmin

On a fresh deploy, the first account to sign up gets access.firstAccountRole once it proves its email address, as long as no account holds a role that grants users:manage. An OAuth or magic-link signup proves it at once; a password signup proves it by opening the verify link in a browser signed in as the account, or through a password reset. The promotion is logged in the account's activity as admin.firstAccountPromoted.

Register your own account before you share the URL, since the first signup wins. The account is not promoted if it came from a guest checkout, is suspended or scheduled for deletion, or already holds a passkey, TOTP or linked provider. layers/kit/server/auth/first-account.ts(Licensees only) has the whole rule.

Set firstAccountRole: false to turn this off. The build refuses a role that is not declared or that doesn't grant users:manage.

Promote an account by hand

Roles live in users.role. Run one statement, against the local database while pnpm dev runs, or against production with the DATABASE_URL from .env.production:

psql postgres://postgres:postgres@127.0.0.1:5432/postgres -c \
  "UPDATE users SET role = 'superadmin' WHERE email = 'you@example.com'"
psql "$DATABASE_URL" -c \
  "UPDATE users SET role = 'superadmin' WHERE email = 'you@example.com'"

If pnpm dev serves PGlite on another port, use that one: the port NUXT_DEV_PGLITE_PORT pins, or the one its startup log names when 5432 was taken. While the dev server runs, .data/pglite-server.json holds the full URL.

To take the role from a stranger who signed up first, set both rows in one statement so there is never a moment with nobody holding users:manage:

psql "$DATABASE_URL" -c \
  "UPDATE users SET role = CASE email WHEN 'you@example.com' THEN 'superadmin' ELSE 'user' END WHERE email IN ('you@example.com', 'them@example.com')"

Then suspend them from the customers page. A change made by hand writes no activity entry.

Configuration

KeyDefaultWhat it does
access.permissionsfour permissionsEvery permission a route may check.
access.defaultRole'user'The role of a new account, and of a stored role the config doesn't declare.
access.firstAccountRole'superadmin'Granted to the deploy's first verified account. false turns it off.
access.rolePermissionsuser, staff, superadminEach role and what it holds.
access.planPermissionsinsights:read on every paid planWhat each plan sells. Needs an entry per plan, plus free.

Good to know

  • A misspelled role in users.role reads as defaultRole and grants nothing, so a typo in a manual promotion looks like nothing happened.
  • A role change applies on the account's next request. The browser keeps showing the old navigation until its session refreshes. To sign the account out as well, call revokeSessions(event, userId).
  • layers/kit/scripts/seed/users.mjs(Licensees only) takes its roles from access: admin@ gets firstAccountRole, or the first role granting users:manage; author@ and the other staff get the first other role granting blog:author; everyone else gets defaultRole. Renamed roles need no edit.
  • A string in access.planPermissions must never also be in orgs.permissions; the build fails if it is.
  • GET /api/users answers 400 to a filter value it doesn't know, such as a role the config doesn't declare, rather than ignoring it.

A link with a lock opens the kit’s private repository, which every licensee can read.