Role-Based Access Control (RBAC)
Security is built into the core of Wecon. You cannot define a route without explicitly stating who can access it.
Defining Roles
First, tell Wecon what roles exist in your system:
typescript
const wecon = new Wecon()
.roles(["admin", "manager", "user", "guest"])
.guestRole("guest") // The role assumed if req.user is missing
.build();Protecting Routes
Assign roles to each route using the roles property.
typescript
new Route({
// ...
roles: ["admin"], // Only admins
});
new Route({
// ...
roles: ["admin", "manager"], // Admins OR Managers
});
new Route({
// ...
roles: ["guest", "user"], // Publicly accessible (if guest is default)
});How it Works
- Request Arrives: Wecon intercepts the request.
- Identify User: It looks for
req.user.roles(an array of strings).- If
req.useris undefined, it assigns theguestRole.
- If
- Match Route: It finds the matching route configuration using the URL.
- Check Permissions: It checks if there is an intersection between the user's roles and the route's allowed roles.
- Decision:
- Authorized: The request proceeds to your middlewares.
- Unauthorized: Wecon immediately returns a
403 Forbidden(or401 Unauthorizedfor guests) with a helpful error message.
Helpful Errors
In development mode, Wecon provides detailed error messages to help you debug permissions.
typescript
// Enable helpful errors
.dev({ helpfulErrors: true })Example 403 Response:
Insufficient permissions to access GET /api/admin/settings
Required roles: admin
Your roles: user