decrypt.middleware.ts
921 Bytes
import { Injectable, NestMiddleware } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request, Response, NextFunction } from 'express';
import { clientDecryptBody, privateDecryptBody } from 'src/helpers/decryptor.helper';
@Injectable()
export class DescryptMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
const { authorization } = req.headers;
if (!authorization) {
next();
}
const token = authorization?.split(' ')[1] || '';
if (token) {
const decodeToken = new JwtService({}).decode(token);
if (decodeToken.type === 'public' && !req.baseUrl.includes('/auth/login')) {
if (Object.keys(req.body).length !== 0) req.body = clientDecryptBody(req.body.data);
} else {
if (Object.keys(req.body).length !== 0) req.body = privateDecryptBody(req.body.data);
}
next();
}
}
}