@prism-engineer/router
Version:
Type-safe Express.js router with automatic client generation
42 lines • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAuthScheme = createAuthScheme;
exports.validateAuth = validateAuth;
function createAuthScheme(config) {
if (!config.name) {
throw new Error('name is required');
}
if (typeof config.name !== 'string') {
throw new Error('name must be a string');
}
if (!config.validate) {
throw new Error('validate function is required');
}
if (typeof config.validate !== 'function') {
throw new Error('validate function must be a function');
}
return config;
}
async function validateAuth(schemes, req) {
const schemesToTry = Array.isArray(schemes) ? schemes : [schemes];
let lastError = null;
for (const scheme of schemesToTry) {
try {
const authContext = await scheme.validate(req);
if (authContext) {
return { name: scheme.name, context: authContext };
}
}
catch (error) {
lastError = error;
// Continue to next scheme if this one fails
continue;
}
}
// If we have an error from validation, throw that; otherwise throw generic message
if (lastError) {
throw lastError;
}
throw new Error('Authentication failed');
}
//# sourceMappingURL=createAuthScheme.js.map