Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
auth (default)
✓ const auth = require('authorized');
✗ import auth from 'authorized'; // ESM not supported; use require()
The package does not ship ESM; use CommonJS require(). No named exports.
UnauthorizedError
✓ const UnauthorizedError = require('authorized').UnauthorizedError;
✗ import { UnauthorizedError } from 'authorized'; // ESM not supported
Access the error constructor via the returned module.
auth.role()
✓ auth.role('admin', function(req, done) { done(null, !!req.user.admin); });
Role getters receive (req, done) for global roles, (entity, req, done) for entity.relation roles.
auth.entity()
✓ auth.entity('organization', function(req, done) { done(null, org); });
Entity getters fetch the entity object based on the request (e.g., from URL params).
auth.can()
✓ auth.can('add members to organization', 'delete organization');
Returns middleware that passes if user has at least one of the listed roles.
auth.view()
✓ const view = auth.view(req); view.has('admin'); view.can('add members');
View object attached to request after auth.can() middleware passes. Provides has(), can(), and actions property.
Complete example of setting up roles, entities, actions, and middleware in an Express app.
const auth = require('authorized');
// Define roles
auth.role('admin', function(req, done) {
done(null, req.user && req.user.admin);
});
auth.role('organization.owner', function(org, req, done) {
if (!req.user) {
done();
} else {
done(null, !!~org.owners.indexOf(req.user.id));
}
});
// Define entity getter
auth.entity('organization', function(req, done) {
const orgId = req.params.orgId;
process.nextTick(function() {
done(null, {id: orgId, owners: ['user.1']});
});
});
// Define actions
auth.action('add members to organization', ['admin', 'organization.owner']);
auth.action('delete organization', ['admin']);
// Create middleware
const canAddMembers = auth.can('add members to organization');
// Use in Express
const express = require('express');
const app = express();
app.post('/organizations/:orgId/members', canAddMembers, function(req, res) {
res.json({ message: 'member added' });
});
app.listen(3000);
Errors
Common errors & fixes
Error: Cannot find module 'authorized'
Missing npm install or incorrect import path.
fixRun `npm install authorized` in the project directory and use require('authorized'). TypeError: auth.can is not a function
auth.action() was called before all roles/entities are defined, or auth object is not initialized.
fixEnsure auth.role() and auth.entity() are called before auth.action(), and verify require('authorized') returns the auth object. UnauthorizedError: Not authorized
User does not have any required roles for the action.
fixCheck role getters: ensure they call done(null, true) for authorized users and done(null, false) otherwise. Also verify entity getters load the correct entity.
Audit
Dependencies
No dependency data recorded yet.