mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
32d2603adb
Add tracking of rate limit method to metrics GitOrigin-RevId: 3996c2a0ccb747018571ce402120be46fc52eace
41 lines
854 B
JavaScript
41 lines
854 B
JavaScript
const { RateLimiter } = require('../../infrastructure/RateLimiter')
|
|
const { promisifyAll } = require('@overleaf/promise-utils')
|
|
|
|
const rateLimiter = new RateLimiter('login', {
|
|
points: 10,
|
|
duration: 120,
|
|
})
|
|
|
|
function processLoginRequest(email, callback) {
|
|
rateLimiter
|
|
.consume(email, 1, { method: 'email' })
|
|
.then(() => {
|
|
callback(null, true)
|
|
})
|
|
.catch(err => {
|
|
if (err instanceof Error) {
|
|
callback(err)
|
|
} else {
|
|
callback(null, false)
|
|
}
|
|
})
|
|
}
|
|
|
|
function recordSuccessfulLogin(email, callback) {
|
|
rateLimiter
|
|
.delete(email)
|
|
.then(() => {
|
|
callback()
|
|
})
|
|
.catch(err => {
|
|
callback(err)
|
|
})
|
|
}
|
|
|
|
const LoginRateLimiter = {
|
|
processLoginRequest,
|
|
recordSuccessfulLogin,
|
|
}
|
|
LoginRateLimiter.promises = promisifyAll(LoginRateLimiter)
|
|
|
|
module.exports = LoginRateLimiter
|