Trying to get auth0 working with NestJS and it’s great that auth0 has sample code.
The main issue I have with it is that I don’t fully understand the authentication middleware code.
@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {
resolve(): MiddlewareFunction {
return (req, res, next) => {
jwt({
secret: expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: 'https://${DOMAIN}/.well-known/jwks.json',
}),
audience: 'http://localhost:3000',
issuer: 'https://${DOMAIN}/',
algorithm: '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();
});
};
}
}
Specifically, I don’t understand why this anonymous function comes immediately after the express-jwt middleware call, without a semi-colon separating it from the express-jwt call.
(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();
});
Anyways, the TS compiler doesn’t like it and I don’t understand it enough to fix it.
I’m trying to use this with the latest NestJS version & TS v3.4.x, and I’m having more trouble because NestJS middleware has been refactored.