Guides

Checking Permissions

Verify user access at runtime

What it does

Checking a permission means asking permly: "Can this user do this action?" You pass a user ID and a permission string, and get back true or false.

When to use it

Check permissions:

  • Before routes execute — guard POST, PUT, DELETE
  • Before showing UI — hide a delete button if user can't delete
  • Before business logic — verify access before the app does anything

Always check before allowing an action.

How it works

  1. Get the user ID from auth (session, JWT, etc.)
  2. Call .can(): const allowed = await perms.user(id).can("posts.delete")
  3. Make a decision based on the result

The user ID can be any value: a number, UUID, email, etc. permly doesn't care—you provide it.

Examples

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// Assign a role and check a permission
await perms.user(42).assignRole("editor");

const allowed = await perms.user(42).can("posts.create");

if (allowed) {
  console.log("User can create posts");
} else {
  console.log("Access denied");
}
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

await perms.user(42).assignRole("editor");

const allowed: boolean = await perms.user(42).can("posts.create");

if (allowed) {
  console.log("User can create posts");
}

In Express routes

Use the built-in middleware to protect routes:

import express from "express";
import { requirePermission } from "permly/express";
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const app = express();

// This route requires posts.create permission
app.post("/posts", requirePermission(perms, "posts.create"), (req, res) => {
  res.json({ created: true });
});
import express from "express";
import { requirePermission } from "permly/express";
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const app = express();

app.post("/posts", requirePermission(perms, "posts.create"), (_req, res) => {
  res.json({ created: true });
});

Check multiple permissions

If a user needs more than one permission, check them all:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// User must have BOTH permissions
const canEdit = await perms.user(42).can("posts.edit");
const canPublish = await perms.user(42).can("posts.publish");

if (canEdit && canPublish) {
  console.log("User can edit and publish");
} else {
  console.log("User lacks one or more permissions");
}
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const canEdit: boolean = await perms.user(42).can("posts.edit");
const canPublish: boolean = await perms.user(42).can("posts.publish");

if (canEdit && canPublish) {
  console.log("User can edit and publish");
}

Async/await

Checks are async because they may hit the database. Always await:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// ✓ Correct: await the check
const allowed = await perms.user(42).can("posts.delete");

// ✗ Wrong: don't forget await
// const allowed = perms.user(42).can("posts.delete"); // Promise, not boolean
import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// Correct: TypeScript requires await for the boolean type
const allowed: boolean = await perms.user(42).can("posts.delete");

Type-safe permission checking

In TypeScript, you can create a typed wrapper to catch permission name typos at compile time:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// Typo! TypeScript doesn't catch this—it's just a string
const allowed = await perms.user(42).can("posts.delet");
// Returns false (permission doesn't exist), not a type error
import { perms, setupPermissions } from "./permly.js";
import { createTypedPermissions } from "./lib/typed-permissions.js";

await setupPermissions();

type MyPermission = "posts.create" | "posts.read" | "posts.edit" | "posts.delete";
type MyRole = "admin" | "editor" | "viewer";

// Create a typed client
const typed = createTypedPermissions<MyPermission, MyRole>(
  perms.user(42)
);

// ✓ Correct: TypeScript accepts this
const allowed = await typed.can("posts.delete");

// ✗ Error: TypeScript catches the typo at compile time
// const typo = await typed.can("posts.delet");

All methods work the same way:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

const userScope = perms.user(42);

// Check single permission
await userScope.can("posts.create");

// Check multiple (any)
await userScope.canAny(["posts.edit", "posts.delete"]);

// Check multiple (all required)
await userScope.canAll(["posts.edit", "posts.publish"]);

// Check role
await userScope.hasRole("editor");

// Get all roles
await userScope.getRoles();
import { perms, setupPermissions } from "./permly.js";
import { createTypedPermissions } from "./lib/typed-permissions.js";

await setupPermissions();

type Permission = "posts.create" | "posts.edit" | "posts.delete" | "posts.publish";
type Role = "admin" | "editor";

const typed = createTypedPermissions<Permission, Role>(perms.user(42));

// All methods have type-safe parameters
await typed.can("posts.create");
await typed.canAny(["posts.edit", "posts.delete"]);
await typed.canAll(["posts.edit", "posts.publish"]);
await typed.hasRole("editor");
await typed.getRoles();

Common mistakes

Forgetting to await — Returns a Promise, not a boolean. Always await.

Checking without assigning a role or permission first — If a user has no roles and no direct permissions, all checks fail.

Typos in permission names — Returns false (permission not found). In TypeScript, define permissions as a union type and use createTypedPermissions() to catch typos at compile time.