From 7121b2f4cd3b93a45cb08ca3b585ac2e5b05e66e Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 14 Aug 2022 22:56:46 +1000 Subject: [PATCH] interceptor error types needed --- client/src/actions.ts | 33 ++++++++++++++------------------ client/src/app.tsx | 5 +++-- client/src/components/header.tsx | 2 +- client/src/routes/login.tsx | 13 +++++++++++-- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/client/src/actions.ts b/client/src/actions.ts index 042d210..f085b1d 100644 --- a/client/src/actions.ts +++ b/client/src/actions.ts @@ -1,53 +1,48 @@ import axios from 'axios'; +import type { AxiosInstance } from 'axios'; const api = axios.create({ baseURL: 'http://localhost:41337', withCredentials: true }); +// Add a response interceptor +api.interceptors.response.use(function onSuccess(response) { + return [null, response.data]; +}, function onError(error) { + return [error.response.data.error, null]; +}); + const actions = { getBundles: async function getBundles() { - const { data: bundles } = await api.get('/bundles'); - return bundles; + return api.get('/bundles'); }, getUserOffers: async function getUserOffers(state, userId) { - const { data: offers } = await api.get(`/user/${userId}/offers`); - return offers; + return api.get(`/user/${userId}/offers`); }, getOffers: async function getOffers(userId) { - const { data } = await api.get(`/user/${userId}/offers`); - return data; + return api.get(`/user/${userId}/offers`); }, getLogin: async function getLogin() { - try { - const { data: user } = await api.get('/auth/login') - console.log(user); - return user; - } catch (e) { - return null; - } + return api.get('/auth/login') }, postLogin: async function postLogin(email, password) { - const { data } = await api.post('/auth/login', { + return api.post('/auth/login', { email, password, // discogs, }); - - return data; }, postRegister: async function postRegister(email, password) { - const { data } = await api.post('/auth/register', { + return api.post('/auth/register', { email, password, }); - - return data; }, }; diff --git a/client/src/app.tsx b/client/src/app.tsx index cff79a7..ef3bbf1 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -19,8 +19,8 @@ function App() { useEffect(() => { async function getLogin() { - const user = await actions.getLogin(); - if (!user) return route('/login'); + const [error, user] = await actions.getLogin(); + if (error) return route('/login'); setUser(user); route('/search'); } @@ -30,6 +30,7 @@ function App() { function loginSuccess(user) { setUser(user); + route('/search'); } return ( diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 08d8572..42c8226 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -6,7 +6,7 @@ const Header = ({ user }) => { ); diff --git a/client/src/routes/login.tsx b/client/src/routes/login.tsx index 2e6b123..17f6e6e 100644 --- a/client/src/routes/login.tsx +++ b/client/src/routes/login.tsx @@ -9,13 +9,22 @@ const Login = ({ success }) => { const [confirmPassword, setConfirmPassword] = useState(''); async function postLogin() { - const user = await actions.postLogin(email, password); + const [error, user] = await actions.postLogin(email, password); + console.log(error, user); + if (error) return console.warn(error); + success(user); + } + + async function postRegister() { + const [error, user] = await actions.postRegister(email, password); + if (error) return console.warn(error); success(user); } function handleSubmit(e) { e.preventDefault(); - postLogin(); + if (mode === 'login') return postLogin() + if (mode === 'register') return postRegister(); } if (mode === 'register') return (