In this guide, we'll explore how to set up a rate-limiting feature for password attempts in Xano. This feature suspends users from logging in for a specified duration if they exceed a certain number of failed login attempts within a given time frame. It helps prevent brute-force attacks and reduces server load from excessive invalid requests.
Start by creating a table called `login_attempts` to log all login attempts. This table should have the following columns:
Additionally, add two columns to your `users` table:
In your `auth/login` endpoint, modify the logic to log each login attempt in the `login_attempts` table. Add a record with the `user_id` and `success` (set to `true` for successful logins and `false` for failed attempts).
// Successful login attempt
add_record(login_attempts, {
user_id: user.id,
success: true
});
// Failed login attempt
add_record(login_attempts, {
user_id: user.id,
success: false
});
Next, create a trigger that checks for failed login attempts within a specific time frame (e.g., 5 attempts in the last 60 seconds). If the condition is met and the user is not already suspended, update the `users` table to set `temp_suspension` to `true` and `temp_timeout_end` to the current timestamp plus a specified duration (e.g., 60 seconds).
sql
query_all_records(login_attempts, {
filters: [
{
field: 'user_id',
operator: 'equals',
value: new.user_id
},
{
field: 'success',
operator: 'equals',
value: false
},
{
field: 'created_at',
operator: 'greater_than',
value: timestamp_subtract(now(), seconds(60))
},
{
field: 'created_at',
operator: 'less_than',
value: now()
}
]
})
if (login_attempt_count >= 5 && get_record(users, new.user_id).temp_suspension == false) {
edit_record(users, new.user_id, {
temp_suspension: true,
temp_timeout_end: timestamp_add(now(), seconds(60))
});
}
This suspends the user for 60 seconds after the threshold is reached.
To prevent suspended users from making further login attempts, create pre-middleware that checks the user's suspension status before executing the login endpoint logic.
user = get_record(users, {email: vars.email});
precondition(user.temp_suspension == false, "Too many requests. Please try again later.", 429);
Finally, create a background task to automatically unsuspend users after the timeout period has expired.
sql
query_all_records(users, {
filters: [
{
field: 'temp_suspension',
operator: 'equals',
value: true
},
{
field: 'temp_timeout_end',
operator: 'less_than',
value: now()
}
]
});
foreach(users, user => {
edit_record(users, user.id, {
temp_suspension: false,
temp_timeout_end: null
});
});
With these steps completed, your Xano application now has a rate-limiting feature for password attempts, improving security and preventing potential brute-force attacks.
This transcript was AI generated to allow users to quickly answer technical questions about Xano.
I found it helpful
I need more support