@tsed/formio
Version:
Formio package for Ts.ED framework
35 lines (27 loc) • 799 B
text/typescript
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
*/
()
export class FormioAuthMiddleware {
()
protected formio: FormioService;
get tokenHandler(): any {
return promisify(this.formio.middleware.tokenHandler);
}
async use(() ctx: Context) {
const req = ctx.getRequest();
const res = ctx.getResponse();
await this.tokenHandler(req, res);
if (!getValue(req, "token.user._id")) {
throw new Unauthorized("User unauthorized");
}
}
}