Added the usual middlewares

This commit is contained in:
Nicolai Ort 2021-03-01 17:24:02 +01:00
parent 9323cecf94
commit e0523db739
2 changed files with 28 additions and 0 deletions

View File

@ -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

View File

@ -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);
}
}