How to add regular middleware to specific routes? #299
-
Just wondering how best to compose any regular express middlewares such as this Looking to include this middleware to a few EZ endpoints, and figure there might be a way to do it without resorting to Thanks! https://auth0.com/docs/quickstart/backend/nodejs/01-authorization#protect-api-endpoints
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @samburgers , thank you for the very interesting question. That Handler has the usual signature for express: (req: Request, res: Response, next: NextFunction) => Promise<void> Request and Response are the entities available to const myJWTMiddleware = createMiddleware({
input: z.object({}),
middleware: async ({request, response}) => {
const next = (e?: Error) => {
if (e) {
throw e;
}
};
await checkJwt(request, response, next);
return { auth: request.auth }; // returns become options for endpoint handler
}
}); I did not try it in action, but it should be something similar. You gave me an interesting idea. I think I will create a special method to facilitate this kind of middlewares. |
Beta Was this translation helpful? Give feedback.
Hello @samburgers ,
thank you for the very interesting question.
According to the documentation,
checkJwt
is theHandler
created byauth()
method, specified here:https://github.com/auth0/node-oauth2-jwt-bearer/blob/0b65192/packages/express-oauth2-jwt-bearer/src/index.ts#L62
That Handler has the usual signature for express:
Request and Response are the entities available to
Middleware
, so you can create one the following way: