Ok, I have an interceptors in my index.js
to refresh the token if the status code is 401
, this works fine, but in login page if I return another status code from the server, the messages errors in the page not working, because the axios
interceptors not receive a 401
.
But if receive a 401
, the interceptors work fine.
This is a screenshot about that. It returns a 404
from the server if not found the user.
The error is related with Login.vue
, but if I delete the axios.interceptors
in my index.js
the "status" in Login.vue
it works fine.
Interceptors
axios.interceptors.response.use(response => {
return response;
}, error => {
if (error.response.status === undefined) {
return;
} else {
const code = error.response.status;
if (code === 401) {
localStorage.removeItem("token");
axios.get("token/refresh", {
params: {
correo: window.localStorage.getItem("email")
}
}).then(response => {
var refresh_token = response.data.token;
localStorage.setItem("token", refresh_token);
}).catch(error => {
const response = error.response;
console.log(response.data.errors);
})
return Promise.reject(error);
}
}
});
I tried in the interceptors, to use something like this:
if(error.response.status == undefined) return;
But it doesn't work.
Login Catch
.catch(error => {
this.loading = false;
if (error.response.status != null) {
switch (error.response.status) {
case 400:
this.alertError = true;
this.errorMessage = "No estás autorizado para acceder.";
this.loading = false;
break;
case 500:
this.alertError = true;
this.errorMessage =
"Hay un problema con el servidor, disculpa las molestias.";
this.loading = false;
break;
case 404:
this.alertError = true;
this.errorMessage = "Vuelve a ingresar tu contraseña";
break;
default:
this.alertError = true;
this.errorMessage =
"Hay un error interno en el servidor, intenta de nuevo más tarde";
}
}
})
How to handle this?
error
variable to find out what properties it contains? (Off the top of my head I think you probably wanterror.status
noterror.response.status
, but it'd be easier to check than to guess.) – Daniel Beck