What it does
A permission is a string that represents an ability: posts.create, posts.edit, admin.settings. You define all permissions your app needs, grant them to roles, and check them when users try to act.
When to use it
Use permissions when you need to:
- Control who can do what — who can post, edit, delete, admin, etc.
- Track these rules in code — permissions live in config, not scattered in if-statements
- Reuse rules across the app — the same permission check works in routes, APIs, UI
How it works
- Define permissions in config:
["posts.create", "posts.edit", "posts.delete"] - Give them to roles:
admingets all three,editorgets create and edit - Assign roles to users
- Check before action:
if (await perms.user(id).can("posts.delete")) { ... }
Permissions are strings. The format is up to you—use dots, slashes, or underscores. Just be consistent.
Naming conventions
Pick a pattern and stick to it:
- Dot notation (common):
posts.create,posts.edit,comments.delete - Slash notation:
posts/create,posts/edit - Underscores:
posts_create,posts_edit
Use a pattern that reads naturally in your app. Dot notation reads like "posts dot create" (a permission in the posts namespace).
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Check if user has a specific permission
const allowed = await perms.user(42).can("posts.edit");
if (allowed) {
console.log("User can edit posts");
} else {
console.log("User cannot edit posts");
}
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const allowed: boolean = await perms.user(42).can("posts.edit");
if (allowed) {
console.log("User can edit posts");
}
Permissions are strings
Permissions are just strings. By default (strict mode), permly validates them at check time—if you check for a permission that doesn't exist in config, it throws PermissionNotFoundError. With strict: false, unknown permissions return false instead.
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Default strict mode: throws PermissionNotFoundError
try {
const allowed = await perms.user(42).can("unknown.permission");
} catch (e) {
console.log(e.name); // PermissionNotFoundError
}
// Or disable strict mode to get false
// const perms = createPermissions({ strict: false, ... });
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
try {
const allowed: boolean = await perms.user(42).can("unknown.permission");
} catch (e: any) {
console.log(e.name);
}
Common mistakes
Typos in permission names — In JavaScript, typos silently return false. In TypeScript with strict mode, typos are caught at compile time.
Checking permissions that aren't in your config — It returns false, so it's safe. But it's usually a sign of a missing definition.
Too many permissions — If you have hundreds of permissions, you've probably gone too fine-grained. Use roles and wildcards instead.
Related pages
- Roles — Group permissions together
- Checking Permissions — How to check at runtime
- Direct Permissions — Assign without roles