Build granular permission system
✓Works with OpenClaudeYou are a backend security architect building a granular permission system for a Node.js/Express application. The user wants to implement role-based access control (RBAC) with resource-level permissions, permission inheritance, and dynamic permission checking.
What to check first
- Verify your project has
express,jsonwebtoken, and a database driver installed:npm list express jsonwebtoken - Check your database schema supports storing roles, permissions, and resource associations
- Confirm you have middleware setup in your Express app to extract and validate JWT tokens
Steps
- Create a permissions database schema with
role,permission, androle_permissionjunction tables; include resource type and resource ID fields for granular control - Define permission constants as a structured object:
{ USER: { CREATE: 'user:create', READ: 'user:read', UPDATE: 'user:update', DELETE: 'user:delete' }, POST: { ... } } - Build a
Permissionmodel class with methods to check single permissions, multiple permissions (AND/OR logic), and resource-scoped permissions - Create an
authorizemiddleware factory that accepts required permissions and returns Express middleware checking both user role and resource ownership - Implement permission caching with TTL (using Redis or in-memory cache) to avoid database hits on every request
- Add permission inheritance so child roles inherit parent permissions through recursive queries
- Build an admin endpoint to assign/revoke permissions dynamically without redeploying
- Test with specific resource IDs: verify user can edit their own post but not others' posts
Code
// permissions.js - Permission engine
const NodeCache = require('node-cache');
const PERMISSIONS = {
USER: {
CREATE: 'user:create',
READ: 'user:read',
UPDATE: 'user:update',
DELETE: 'user:delete'
},
POST: {
CREATE: 'post:create',
READ: 'post:read',
UPDATE: 'post:update',
DELETE: 'post:delete'
},
ADMIN: {
MANAGE_ROLES: 'admin:manage_roles',
MANAGE_PERMISSIONS: 'admin:manage_permissions'
}
};
class PermissionManager {
constructor(db) {
this.db = db;
this.cache = new NodeCache({ stdTTL: 3600 }); // 1 hour TTL
}
async getUserPermissions(userId) {
const cached = this.cache.get(`user_perms_${userId}`);
if (cached) return cached;
const query = `
SELECT DISTINCT p.permission_code, p.resource_type, rp.resource_id
FROM users u
JOIN user_roles ur ON u.id = ur.user_id
JOIN roles r ON ur.role_id = r.id
JOIN role_permissions rp ON r.id = rp.role_id
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Authentication Skills
Other Claude Code skills in the same category — free to download.
JWT Auth
Implement JWT authentication from scratch
OAuth Setup
Set up OAuth 2.0 with multiple providers
NextAuth Setup
Configure NextAuth.js/Auth.js
Passport Setup
Set up Passport.js with strategies
Magic Link Auth
Implement passwordless magic link auth
Two Factor Auth
Add 2FA/MFA to authentication flow
Session Management
Implement secure session management
API Key Auth
Create API key authentication system
Want a Authentication skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.