Skip to content

Commit

Permalink
Added auto logout
Browse files Browse the repository at this point in the history
  • Loading branch information
BahirHakimy committed Jul 30, 2023
1 parent f33ec7d commit ba5e96f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/javascript/utils/api.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import axios from 'axios';
import { clearSession, getUser } from './auth';
import { clearSession } from './auth';

const authFetch = (token) =>
axios.create({
const authFetch = (token) => {
const instance = axios.create({
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
Accept: 'application/json',
},
});
instance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status === 401) {
clearSession();
window.location.reload();
}
return Promise.reject(error);
}
);
return instance;
};

export { authFetch as axios };

0 comments on commit ba5e96f

Please sign in to comment.