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

Woooohooo teamwork makes the dreamwork!

Hi! thanks for this post, It really help me out! I have only one concern: Using this middleware, Are the claims inside the token validated?

This topic was automatically closed 26 days after the last reply. New replies are no longer allowed.

Hi, I’ve run the command npm install express-jwt jwks-rsa dotenv and copied the authentication.middleware.ts exactly from the post. But I see the error-

src/common/authentication.middleware.ts:11:5 - error TS2349: This expression is not callable.
  Type 'typeof import("D:/nestJS/blog-backend/node_modules/express-jwt/dist/index")' has no call signatures.

11     jwt({

My resource versions:

"dotenv": "^16.0.1",
"express-jwt": "^7.7.5",
"jwks-rsa": "^2.1.4",

I’ve applied multiple solutions for StackOverFlow and tried jwt-express official website. But no solution yet.

How can I solve it?

Your version of express-jwt is newer than that used in the article. Try installing “express-jwt”: “^6.0.0” and see if that works.

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();
});
}
}`

Is there a guide similar to this on how to get the role assigned to a user?