Skip to content

Instantly share code, notes, and snippets.

@yajra
Last active September 20, 2023 06:24
Show Gist options
  • Save yajra/5f5551649b20c8f668aec48549ef5c1f to your computer and use it in GitHub Desktop.
Save yajra/5f5551649b20c8f668aec48549ef5c1f to your computer and use it in GitHub Desktop.
Axios 401 response interceptor.
// Add a 401 response interceptor
window.axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
if (401 === error.response.status) {
swal({
title: "Session Expired",
text: "Your session has expired. Would you like to be redirected to the login page?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
closeOnConfirm: false
}, function(){
window.location = '/login';
return Promise.reject(error)
});
} else {
return Promise.reject(error);
}
});
@yajra
Copy link
Author

yajra commented Jul 19, 2023

Gist adjusted and included the reject call. Thanks!

@HogansJoe
Copy link

Awesome, great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment