From b09aa0debfd2c3f07e0ff7dfdd867d9e5e83a2a1 Mon Sep 17 00:00:00 2001 From: "Lin, Hung-Shin" Date: Thu, 2 Nov 2023 11:36:42 +0800 Subject: [PATCH] feat: refactor frontend auth error handling --- app/src/utils/auth.js | 130 ++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 80 deletions(-) diff --git a/app/src/utils/auth.js b/app/src/utils/auth.js index 8dccb81..4094066 100644 --- a/app/src/utils/auth.js +++ b/app/src/utils/auth.js @@ -12,102 +12,72 @@ export async function isLoggedIn() { return loggedIn } -export const login = async (username, password) => { - try { - 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 - } +function handleRequestError(err) { + if (!err.response) throw err // Not an axios error + const { status } = err.response + if (status === 429) { + throw new TooManyRequestsError(err.response.data.message) } + if (status === 401) { + throw new UnauthorizedError(err.response.data.message) + } + if (status === 400) { + const { errors } = err.response.data + if (Array.isArray(errors) && errors.length > 0) { + const { path, msg } = errors[0] + switch (path) { + case 'username': + throw new ValidationError('Username is already taken') + case 'email': + throw new ValidationError('Email is already taken') + case 'password': + throw new ValidationError('Password is too weak') + default: + throw new ValidationError(msg) + } + } + throw new ValidationError(err.response.data.message) + } + throw err } -export const register = async (username, email, password) => { - try { - await api.post('/auth/register', { +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, }) - return true - } catch (err) { - if (!err.response) throw err - const { status } = err.response - if (status === 429) { - throw new TooManyRequestsError(err.response.data.message) - } - if (status === 401) { - throw new UnauthorizedError(err.response.data.message) - } - if (status === 400) { - const { errors } = err.response.data - if (Array.isArray(errors) && errors.length > 0) { - const { path, msg } = errors[0] - switch (path) { - case 'username': - throw new ValidationError('Username is already taken') - case 'email': - throw new ValidationError('Email is already taken') - case 'password': - throw new ValidationError('Password is too weak') - default: - throw new ValidationError(msg) - } - } - throw new ValidationError(err.response.data.message) - } - throw err - } + .then(() => true) + .catch(handleRequestError) } export const checkUsername = async (username) => { - try { - await api.get('/auth/check-username', { + return api + .get('/auth/check-username', { params: { username }, }) - return true - } catch (err) { - 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 - } + .then(() => true) + .catch(handleRequestError) } export const chooseUsername = async (username) => { - try { - await api.post('/auth/register-from-session', { username }) - } catch (err) { - if (!err.response) throw err - 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 - } - } + return api + .post('/auth/register-from-session', { username }) + .then(() => true) + .catch(handleRequestError) } export const logout = () => { - return api.post('/auth/logout') + return api + .post('/auth/logout') + .then(() => true) + .catch(handleRequestError) }