mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +00:00
chore: combine scattered errors into a single object
This commit is contained in:
@@ -107,18 +107,11 @@ const login = async (
|
||||
|
||||
if (!validPass) {
|
||||
const retrySecs = await rateLimiter.consume(req.ip, user?.username)
|
||||
if (retrySecs > 0) {
|
||||
throw {
|
||||
code: 429,
|
||||
message: `Too Many Requests! Retry after ${convertSecondsToHms(
|
||||
retrySecs
|
||||
)}`
|
||||
}
|
||||
}
|
||||
if (retrySecs > 0) throw errors.tooManyRequests(retrySecs)
|
||||
}
|
||||
|
||||
if (!user) throw { code: 401, message: 'Username is not found.' }
|
||||
if (!validPass) throw { code: 401, message: 'Invalid Password.' }
|
||||
if (!user) throw errors.userNotFound
|
||||
if (!validPass) throw errors.invalidPassword
|
||||
|
||||
// Reset on successful authorization
|
||||
rateLimiter.resetOnSuccess(req.ip, user.username)
|
||||
@@ -199,3 +192,18 @@ interface AuthorizeResponse {
|
||||
*/
|
||||
code: string
|
||||
}
|
||||
|
||||
const errors = {
|
||||
invalidPassword: {
|
||||
code: 401,
|
||||
message: 'Invalid Password.'
|
||||
},
|
||||
userNotFound: {
|
||||
code: 401,
|
||||
message: 'Username is not found.'
|
||||
},
|
||||
tooManyRequests: (seconds: number) => ({
|
||||
code: 429,
|
||||
message: `Too Many Requests! Retry after ${convertSecondsToHms(seconds)}`
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user