streamby-core
Version:
StreamBy middleware framework for media storage management
31 lines (30 loc) • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.authenticate = void 0;
exports.checkRole = checkRole;
const authenticate = (config) => async (req, res, next) => {
if (req.path.includes('/public-export/')) {
return next();
}
try {
const auth = await config.authProvider(req);
if (!auth || !auth.userId || !auth.role) {
return res.status(401).json({ error: 'Unauthorized' });
}
req.auth = auth;
next();
}
catch (error) {
console.error('Authentication failed:', error);
return res.status(401).json({ error: 'Unauthorized' });
}
};
exports.authenticate = authenticate;
function checkRole(auth, required) {
const roles = ['viewer', 'editor', 'admin'];
const userIdx = roles.indexOf(auth.role);
const requiredIdx = roles.indexOf(required);
if (userIdx < requiredIdx) {
throw new Error(`Insufficient role: requires ${required}, found ${auth.role}`);
}
}