More robust error handling

This commit is contained in:
oitsjustjose 2020-12-29 11:22:19 -06:00
commit 88277d2d3f
2 changed files with 13 additions and 6 deletions

View file

@ -9,12 +9,15 @@ export default (data) => {
return axios.post(path, data) return axios.post(path, data)
.then((resp) => { .then((resp) => {
const token = resp.data; if (resp.status === 200) {
localStorage.setItem('auth-token', token); const token = resp.data;
localStorage.setItem('auth-token', token);
const user = jwtDecode(token); const user = jwtDecode(token);
store.dispatch({ type: 'SET_USER', user: user._doc }); store.dispatch({ type: 'SET_USER', user: user._doc });
setAuthToken(token); setAuthToken(token);
}
return resp;
}); });
}; };

View file

@ -27,7 +27,11 @@ const LoginComponent = ({
const submit = (evt) => { const submit = (evt) => {
evt.preventDefault(); evt.preventDefault();
postLogin(state).catch((ex) => { postLogin(state).then((resp) => {
if (resp.status !== 200) {
setError(`${resp.data}`);
}
}).catch((ex) => {
setError(`${ex.response.data}`); setError(`${ex.response.data}`);
}); });
}; };