You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
check and apply the rate limit against IP address IF authorization token is bad/not-supplied etc.
check and apply the rate limit against user id IF authorization token is good
I decorate my fastify instance with something like:
fastify.decorate('authenticate',asyncfunction(request,reply){consttoken=request.headers.authorization?.replace('Bearer ','');if(!token){// ###// WANT TO CHECK RATE LIMIT HERE AGAINST IP// ###returnreply.code(401).send(createFailResponseApiModel('68b59dd8d73f85a46a49a18d','No token provided'));}constverifyAccessTokenResult=verifyAccessToken(token);if(!verifyAccessTokenResult.success){// ###// WANT TO CHECK RATE LIMIT HERE AGAINST IP// ###returnreply.code(401).send(createFailResponseApiModel('68b59e0b91a929648f0fb03b','Invalid token'));}constsessionResult=getSessionFromUserClaims(verifyAccessTokenResult.data,);if(!sessionResult.success){// ###// WANT TO CHECK RATE LIMIT HERE AGAINST IP// ###returnreply.code(401).send(createFailResponseApiModel('68b59dfdb827d5a7354aaf33','Invalid session'));}request.session=sessionResult.data;// ###// WANT TO CHECK RATE LIMIT HERE AGAINST USER ID// ###returnundefined;});
If I use the plugin as written I have the option for setting a hook to preHandler or onRequest.
Both don't seem to apply well here, onRequest will trigger before my auth thus only IP is available. Whilst preHandler triggers after my auth and by that point its too late.
Maybe I'm missing something, be happy to hear of solutions?
Activity
metcoder95 commentedon Nov 2, 2025
You can set your authentication hook on the
onRequesthook before registering therate-limitplugin.The
onRequesthook already has access to the headers of the request, but not the body