-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Passing additional data to Middleware #372
Comments
You can implement it like this:
export const authenticateToken = (/* PARAMS HERE */) => {
return async (req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers.authorization;
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return next(new AppError(HttpStatusCode.UNAUTHORIZED, 'error', 'No token'));
}
verify(token, process.env.ACCESS_TOKEN_SECRET, (error, user) => {
if (error) {
console.error(error);
next(new AppError(HttpStatusCode.FORBIDDEN, 'error', 'Invalid or expired token'));
}
req.body.user = user?.user as IJwtUserPayload;
next();
});
};
}; |
I don't think it's possible with |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
I am using
The code I am working on can be seen at this Link
The Problem
I need to pass an additional parameter to the Middleware so that it will act as a factory,
Example, I need to pass a role onto a middleware so that the system can check if the user has permission to access the API
an example we need to create a middleware as follows
And the middleware is called by a controller
How can this be achieved, this is a common scenario, that has to be used. Is it that, the feature is not supported.
The text was updated successfully, but these errors were encountered: