mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
feat: add rate limit for authentication
* improve error handling in frontend
This commit is contained in:
1 parent
203114cd5e
commit
f65b72c5b4
10 files changed
+146
-42
No files matched your search
@@ -2,14 +2,30 @@
|
||||
import AuthForm from '../components/AuthForm.vue'
|
||||
import Background from '../components/DynamicBackground.vue'
|
||||
|
||||
import {
|
||||
TooManyRequestsError,
|
||||
UnauthorizedError,
|
||||
ValidationError,
|
||||
} from '../utils/errors'
|
||||
import { login } from '../utils/auth'
|
||||
|
||||
const handleLogin = async ({ username, password, success, error }) => {
|
||||
try {
|
||||
const isSuccess = await login(username, password)
|
||||
if (isSuccess) success()
|
||||
else error('Wrong username or password.')
|
||||
} catch (err) {
|
||||
if (err instanceof TooManyRequestsError) {
|
||||
error('Too many requests. Try again in 2 minutes.')
|
||||
return
|
||||
}
|
||||
if (err instanceof UnauthorizedError) {
|
||||
error('Wrong username or password.')
|
||||
return
|
||||
}
|
||||
if (err instanceof ValidationError) {
|
||||
error('Invalid username or password.')
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
error('Something went wrong.')
|
||||
}
|
||||
|
||||
@@ -7,25 +7,29 @@ import Background from '../components/DynamicBackground.vue'
|
||||
|
||||
import api from '../utils/api'
|
||||
import { register } from '../utils/auth'
|
||||
import {
|
||||
UnauthorizedError,
|
||||
TooManyRequestsError,
|
||||
ValidationError,
|
||||
} from '../utils/errors'
|
||||
|
||||
function handleRegister({ username, email, password, success, error }) {
|
||||
register(username, email, password)
|
||||
.then(success)
|
||||
.catch((err) => {
|
||||
if (err.response?.status === 409) {
|
||||
// Handle username or email already exists error here
|
||||
if (err.response.data.type === 'username') {
|
||||
error('Username already exists.')
|
||||
} else if (err.response.data.type === 'email') {
|
||||
error('Email already exists.')
|
||||
} else {
|
||||
error('Something went wrong.')
|
||||
}
|
||||
} else if (err.response?.status === 400) {
|
||||
error('Invalid username or email.')
|
||||
} else {
|
||||
console.error(err)
|
||||
if (err instanceof TooManyRequestsError) {
|
||||
error('Too many requests. Try again in 2 minutes.')
|
||||
return
|
||||
}
|
||||
if (err instanceof UnauthorizedError) {
|
||||
error('Invalid username or password.')
|
||||
return
|
||||
}
|
||||
if (err instanceof ValidationError) {
|
||||
error(err.message)
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user