diff --git a/src/middlewares/AuthChecker.ts b/src/middlewares/AuthChecker.ts new file mode 100644 index 0000000..e4ede50 --- /dev/null +++ b/src/middlewares/AuthChecker.ts @@ -0,0 +1,14 @@ +import { Action } from "routing-controllers"; +import { config } from '../config'; + +/** + * Handles authentication via jwt's (Bearer authorization header) for all api endpoints using the @Authorized decorator. + * @param action Routing-Controllers action object that provides request and response objects among other stuff. + * @param permissions The permissions that the endpoint using @Authorized requires. + */ +const AuthChecker = async (action: Action) => { + const provided_token = action.request.query.key; + return provided_token == config.api_key; +} + +export default AuthChecker \ No newline at end of file diff --git a/src/middlewares/ErrorHandler.ts b/src/middlewares/ErrorHandler.ts new file mode 100644 index 0000000..71ef2db --- /dev/null +++ b/src/middlewares/ErrorHandler.ts @@ -0,0 +1,14 @@ +import { ExpressErrorMiddlewareInterface, Middleware } from "routing-controllers"; + +/** + * Our Error handling middlware that returns our custom httperrors to the user. + */ +@Middleware({ type: "after" }) +export class ErrorHandler implements ExpressErrorMiddlewareInterface { + public error(error: any, request: any, response: any, next: (err: any) => any) { + if (response.headersSent) { + return; + } + response.json(error); + } +}