Skip to content

Commit

Permalink
feat: Axios instance 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jhynsoo committed Jul 19, 2024
1 parent 2a7d0a6 commit 5006b8f
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions frontend/src/utils/axios/instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axios from 'axios';

const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;

const instance = axios.create({
baseURL: import.meta.env.VITE_API_URL,
timeout: 1000,
headers: { 'X-Custom-Header': 'foobar' },
});

instance.interceptors.request.use((config) => {
const accessToken = sessionStorage.getItem(ACCESS_TOKEN_KEY);

if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
}

return config;
});

instance.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status !== 401) {
return Promise.reject(error);
}

// TODO: api url update
const REFRESH_API_URL = '/refresh-token';

return instance.post(REFRESH_API_URL).then((response) => {
const { accessToken } = response.data;

sessionStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
error.config.headers.Authorization = `Bearer ${accessToken}`;
return instance(error.config);
});
}
);

export default instance;

0 comments on commit 5006b8f

Please sign in to comment.