/* * Copyright (c) 2022. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ import {Middleware} from "@decorators/express"; import {UnauthorizedError} from '@typescript-error/http'; import {NextFunction, Request, Response} from "express"; import {ExpressNextFunction, ExpressRequest, ExpressResponse} from "../type"; import {errorMiddleware} from "./error"; export function forceLoggedIn(req: ExpressRequest, res: ExpressResponse, next: ExpressNextFunction) { if (typeof req.userId === 'undefined') { return errorMiddleware(new UnauthorizedError(), req, res, next); } next(); } export class ForceLoggedInMiddleware implements Middleware { public use(request: Request, response: Response, next: NextFunction) { return forceLoggedIn(request, response, next); } }