type-graphql
Version:
Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
23 lines (22 loc) • 821 B
JavaScript
import { AuthenticationError, AuthorizationError } from "../errors/index.js";
export function AuthMiddleware(authChecker, container, authMode, roles) {
return async (action, next) => {
let accessGranted;
if (authChecker.prototype) {
const authCheckerInstance = await container.getInstance(authChecker, action);
accessGranted = await authCheckerInstance.check(action, roles);
}
else {
accessGranted = await authChecker(action, roles);
}
if (!accessGranted) {
if (authMode === "null") {
return null;
}
if (authMode === "error") {
throw roles.length === 0 ? new AuthenticationError() : new AuthorizationError();
}
}
return next();
};
}