Skip to content

Commit 812f789

Browse files
committed
Merge branch 'fix/login-rate-limit-only-failed' of github.com:commonknowledge/ts-mapped into fix/login-rate-limit-only-failed
2 parents dc77b57 + 6c8c2bf commit 812f789

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/server/utils/ratelimit.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,20 @@ export async function checkLoginAttempt(ip: string): Promise<boolean> {
3535

3636
export async function rollbackLoginAttempt(ip: string): Promise<void> {
3737
const redis = getClient();
38-
await redis.decr(`rate_limit:login:${ip}`);
38+
const key = `rate_limit:login:${ip}`;
39+
40+
const results = await redis
41+
.multi()
42+
.decr(key)
43+
.expire(key, WINDOW_SECONDS)
44+
.exec();
45+
46+
const newCount =
47+
results && results[0] && Array.isArray(results[0]) ? (results[0][1] as number) : 0;
48+
49+
if (newCount <= 0) {
50+
await redis.del(key);
51+
}
3952
}
4053

4154
export async function checkForgotPasswordAttempt(ip: string): Promise<boolean> {

0 commit comments

Comments
 (0)