mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: refactor frontend auth error handling
This commit is contained in:
1 parent
bcb9f1f803
commit
b09aa0debf
1 file changed
+31
-61
+31
-61
@@ -12,39 +12,8 @@ export async function isLoggedIn() {
|
|||||||
return loggedIn
|
return loggedIn
|
||||||
}
|
}
|
||||||
|
|
||||||
export const login = async (username, password) => {
|
function handleRequestError(err) {
|
||||||
try {
|
if (!err.response) throw err // Not an axios error
|
||||||
await api.post('/auth/login', { username, password })
|
|
||||||
return true
|
|
||||||
} catch (err) {
|
|
||||||
if (!err.response) throw err
|
|
||||||
const { status } = err.response
|
|
||||||
switch (status) {
|
|
||||||
case 401:
|
|
||||||
throw new UnauthorizedError(err.response.data.message)
|
|
||||||
|
|
||||||
case 429:
|
|
||||||
throw new TooManyRequestsError(err.response.data.message)
|
|
||||||
|
|
||||||
case 400:
|
|
||||||
throw new ValidationError(err.response.data.message)
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const register = async (username, email, password) => {
|
|
||||||
try {
|
|
||||||
await api.post('/auth/register', {
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
email,
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
} catch (err) {
|
|
||||||
if (!err.response) throw err
|
|
||||||
const { status } = err.response
|
const { status } = err.response
|
||||||
if (status === 429) {
|
if (status === 429) {
|
||||||
throw new TooManyRequestsError(err.response.data.message)
|
throw new TooManyRequestsError(err.response.data.message)
|
||||||
@@ -71,43 +40,44 @@ export const register = async (username, email, password) => {
|
|||||||
}
|
}
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const login = (username, password) => {
|
||||||
|
return api
|
||||||
|
.post('/auth/login', { username, password })
|
||||||
|
.then(() => true)
|
||||||
|
.catch(handleRequestError)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const register = (username, email, password) => {
|
||||||
|
return api
|
||||||
|
.post('/auth/register', {
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
email,
|
||||||
|
})
|
||||||
|
.then(() => true)
|
||||||
|
.catch(handleRequestError)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const checkUsername = async (username) => {
|
export const checkUsername = async (username) => {
|
||||||
try {
|
return api
|
||||||
await api.get('/auth/check-username', {
|
.get('/auth/check-username', {
|
||||||
params: { username },
|
params: { username },
|
||||||
})
|
})
|
||||||
return true
|
.then(() => true)
|
||||||
} catch (err) {
|
.catch(handleRequestError)
|
||||||
if (!err.response) throw err
|
|
||||||
const { status } = err.response
|
|
||||||
if (status === 429) {
|
|
||||||
throw new TooManyRequestsError('Too many requests')
|
|
||||||
}
|
|
||||||
if (status === 409) {
|
|
||||||
throw new ValidationError('Username is already taken')
|
|
||||||
}
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const chooseUsername = async (username) => {
|
export const chooseUsername = async (username) => {
|
||||||
try {
|
return api
|
||||||
await api.post('/auth/register-from-session', { username })
|
.post('/auth/register-from-session', { username })
|
||||||
} catch (err) {
|
.then(() => true)
|
||||||
if (!err.response) throw err
|
.catch(handleRequestError)
|
||||||
switch (err.response?.status) {
|
|
||||||
case 400:
|
|
||||||
throw new ValidationError('Username is already taken')
|
|
||||||
case 401:
|
|
||||||
throw new UnauthorizedError('You are not a new user')
|
|
||||||
default:
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const logout = () => {
|
export const logout = () => {
|
||||||
return api.post('/auth/logout')
|
return api
|
||||||
|
.post('/auth/logout')
|
||||||
|
.then(() => true)
|
||||||
|
.catch(handleRequestError)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user