Modern Full-Stack Development with Nest.js, React, TypeScript, and MongoDB: Part 1

Try this :slight_smile:

`import { Injectable, NestMiddleware } from ‘@nestjs/common’;
import { expressjwt, GetVerificationKey } from “express-jwt”;
import { expressJwtSecret } from ‘jwks-rsa’;
import { Request, Response } from ‘express’;
import { ConfigService } from “@nestjs/config”;
// require(‘dotenv’).config();

@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {
constructor(private readonly configService: ConfigService) {}
use(req: Request, res: Response, next: Function) {
expressjwt({
secret: expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: https://${this.configService.get('auth_domain')}/.well-known/jwks.json,
}) as GetVerificationKey,
issuer: https://${this.configService.get('auth_domain')}/,
algorithms: [‘RS256’],
})(req, res, (err) => {
if (err) {
const status = err.status || 500;
const message = err.message || ‘Sorry we were unable to process your request.’;
return res.status(status).send({
message,
});
}
next();
});
}
}`