UNPKG

@tsed/formio

Version:
40 lines (39 loc) 1.3 kB
import { __decorate, __metadata, __param } from "tslib"; import { promisify } from "node:util"; import { getValue } from "@tsed/core"; import { Inject } from "@tsed/di"; import { Unauthorized } from "@tsed/exceptions"; import { Middleware } from "@tsed/platform-middlewares"; import { Context } from "@tsed/platform-params"; import { FormioService } from "../services/FormioService.js"; /** * @middleware * @formio */ let FormioAuthMiddleware = class FormioAuthMiddleware { get tokenHandler() { return promisify(this.formio.middleware.tokenHandler); } async use(ctx) { const req = ctx.getRequest(); const res = ctx.getResponse(); await this.tokenHandler(req, res); if (!getValue(req, "token.user._id")) { throw new Unauthorized("User unauthorized"); } } }; __decorate([ Inject(), __metadata("design:type", FormioService) ], FormioAuthMiddleware.prototype, "formio", void 0); __decorate([ __param(0, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], FormioAuthMiddleware.prototype, "use", null); FormioAuthMiddleware = __decorate([ Middleware() ], FormioAuthMiddleware); export { FormioAuthMiddleware };