Reputation: 3543
I have a route like this to execute a function:
router.post('/send-verification-again', sendVerificationAgain);
At the function above we have the email or phone number of the account in which we want to verify.
This will execute a function to send an email or sms to the user with his/her verification code ok?
Apparently we don't want the user to be able to send more than one request per minute to this route, right?
How can I limit execution of a function for specified user at one per minute?
function sendVerificationAgain(email) {
// for each specific email here the function can be executed one time per minute
}
This is very common functionality, is there any convention to do this?
Upvotes: 2
Views: 151
Reputation: 8718
You can simply store the email for a minute and check if it's stored:
const recentlySent = new Set();
function sendVerificationAgain(email) {
if (recentlySent.has(email)) return;
recentlySent.add(email);
setTimeout(() => recentlySent.delete(email), 60e3);
// ...
}
You might also want to first normalize the email, e.g. lowercase it.
For more information, look up "debounce" in the programming sense
Upvotes: 3