import { Injectable, NestMiddleware } from '@nestjs/common'; import { NextFunction, Request, Response } from 'express'; import { UserService } from 'src/modules/objects/user/user.service'; @Injectable() export class UserMiddleware implements NestMiddleware { constructor(private readonly userService: UserService) {} async use(req: Request, res: Response, next: NextFunction) { if (req.session.user) { // TODO: Cache user requests // Might not be a big deal though, there is no expected volume in visitors // TODO: check for bans const userObj = await this.userService.getByUUID(req.session.user, [ 'picture', ]); if (userObj && userObj.activated) { req.user = userObj; } else { delete req.session.user; } } next(); } }