How to reset number of attempts for user who succeeded in his request to log in with express-rate-limit

Viewed 34

I'm trying to implement a rate limiter for my site with express-rate-limit and I cannot seem to find how to reset the counter of attempts for user who made a successful request. My code:

const loginLimiter = rateLimit({
    windowMs: 60 * 60 * 1000, 
    max: 4, 
    standardHeaders: true, 
    legacyHeaders: false,
    handler: function (req, res /*next*/) {
        return res.status(429).json({
            message: 'Too many failed login attempts, please try again later',
        });
    },
    skipSuccessfulRequests: true,
});


router.post(
            '/login',
            loginLimiter,
            async (
                req: Request,
                res: Response,
                next: NextFunction,
            ): Promise<void> => {
            
            //(some code)
            
            }
           );

Ok nvm. I figured it out you just need to add:

router.post(
            '/login',
            loginLimiter,
            async (
                req: Request,
                res: Response,
                next: NextFunction,
            ): Promise<void> => {

            //---(solution)---
            loginLimiter.resetKey(req.ip)
            //---(solution)---
            
            //(some code)
            
            }
           );

I'm not deleting this question for people who may have encountered a similar problem.

0 Answers
Related