interceptor error types needed

This commit is contained in:
ntr committed 2022-08-14 22:56:46 +10:00
1 parent b0bca74ee2
commit 7121b2f4cd
4 files changed
+29 -24

No files matched your search

+14 -19
View File
@@ -1,53 +1,48 @@
import axios from 'axios'; import axios from 'axios';
import type { AxiosInstance } from 'axios';
const api = axios.create({ const api = axios.create({
baseURL: 'http://localhost:41337', baseURL: 'http://localhost:41337',
withCredentials: true 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 = { const actions = {
getBundles: async function getBundles() { getBundles: async function getBundles() {
const { data: bundles } = await api.get('/bundles'); return api.get('/bundles');
return bundles;
}, },
getUserOffers: async function getUserOffers(state, userId) { getUserOffers: async function getUserOffers(state, userId) {
const { data: offers } = await api.get(`/user/${userId}/offers`); return api.get(`/user/${userId}/offers`);
return offers;
}, },
getOffers: async function getOffers(userId) { getOffers: async function getOffers(userId) {
const { data } = await api.get(`/user/${userId}/offers`); return api.get(`/user/${userId}/offers`);
return data;
}, },
getLogin: async function getLogin() { getLogin: async function getLogin() {
try { return api.get('/auth/login')
const { data: user } = await api.get('/auth/login')
console.log(user);
return user;
} catch (e) {
return null;
}
}, },
postLogin: async function postLogin(email, password) { postLogin: async function postLogin(email, password) {
const { data } = await api.post('/auth/login', { return api.post('/auth/login', {
email, email,
password, password,
// discogs, // discogs,
}); });
return data;
}, },
postRegister: async function postRegister(email, password) { postRegister: async function postRegister(email, password) {
const { data } = await api.post('/auth/register', { return api.post('/auth/register', {
email, email,
password, password,
}); });
return data;
}, },
}; };
+3 -2
View File
@@ -19,8 +19,8 @@ function App() {
useEffect(() => { useEffect(() => {
async function getLogin() { async function getLogin() {
const user = await actions.getLogin(); const [error, user] = await actions.getLogin();
if (!user) return route('/login'); if (error) return route('/login');
setUser(user); setUser(user);
route('/search'); route('/search');
} }
@@ -30,6 +30,7 @@ function App() {
function loginSuccess(user) { function loginSuccess(user) {
setUser(user); setUser(user);
route('/search');
} }
return ( return (
+1 -1
View File
@@ -6,7 +6,7 @@ const Header = ({ user }) => {
<nav> <nav>
<a href="/search">Offers</a> <a href="/search">Offers</a>
<a href="/search">Search</a> <a href="/search">Search</a>
<a href={`/user/${user.id}`}>User</a> <a href={`/user/${user.id}`}>List</a>
</nav> </nav>
); );
+11 -2
View File
@@ -9,13 +9,22 @@ const Login = ({ success }) => {
const [confirmPassword, setConfirmPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState('');
async function postLogin() { 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); success(user);
} }
function handleSubmit(e) { function handleSubmit(e) {
e.preventDefault(); e.preventDefault();
postLogin(); if (mode === 'login') return postLogin()
if (mode === 'register') return postRegister();
} }
if (mode === 'register') return ( if (mode === 'register') return (