Skip to content

Commit

Permalink
πŸ”§ feat: api 톡신 μœ„ν•œ axios instance μ„€μ • μΆ”κ°€ #1
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMirror21 committed Oct 16, 2024
1 parent 20d57a8 commit 5cbb712
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getAccessToken } from '@/utils/auth';
import axios, { AxiosInstance } from 'axios';

/**
* Axios μΈμŠ€ν„΄μŠ€μ— 인터셉터λ₯Ό μ„€μ •ν•˜λŠ” ν•¨μˆ˜
*
* @function setInterceptors
* @param {AxiosInstance} instance - 인터셉터λ₯Ό μ„€μ •ν•  Axios μΈμŠ€ν„΄μŠ€
* @returns {AxiosInstance} 인터셉터가 μ„€μ •λœ Axios μΈμŠ€ν„΄μŠ€
*
* @description
* 이 ν•¨μˆ˜λŠ” μš”μ²­ 인터셉터λ₯Ό μ„€μ •ν•˜μ—¬ 각 API μš”μ²­μ— 인증 토큰을 μΆ”κ°€ν•©λ‹ˆλ‹€.
* ν΄λΌμ΄μ–ΈνŠΈ μ‚¬μ΄λ“œμ—μ„œλ§Œ 토큰을 μΆ”κ°€ν•˜λ©°, μ„œλ²„ μ‚¬μ΄λ“œ λ Œλ”λ§ μ‹œμ—λŠ” 토큰을 μΆ”κ°€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
*/
function setInterceptors(instance: AxiosInstance) {
instance.interceptors.request.use(
(config) => {
if (typeof window !== 'undefined' && config.headers) {
config.headers.Authorization = `Bearer ${getAccessToken()}`;
}
return config;
},
(error) => {
return Promise.reject(error);
},
);

return instance;
}

/**
* 인증이 ν•„μš”ν•œ API μš”μ²­μ„ μœ„ν•œ Axios μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜
*
* @function createInstance
* @returns {AxiosInstance} 인터셉터가 μ„€μ •λœ Axios μΈμŠ€ν„΄μŠ€
*
* @description
* 이 ν•¨μˆ˜λŠ” κΈ°λ³Έ URL이 μ„€μ •λœ Axios μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜κ³ ,
* setInterceptors ν•¨μˆ˜λ₯Ό 톡해 인증 토큰을 μžλ™μœΌλ‘œ μΆ”κ°€ν•˜λŠ” 인터셉터λ₯Ό μ„€μ •ν•©λ‹ˆλ‹€.
*/
function createInstance() {
const instance = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
});
return setInterceptors(instance);
}

/**
* 인증이 ν•„μš”ν•˜μ§€ μ•Šμ€ API μš”μ²­μ„ μœ„ν•œ Axios μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜
*
* @function createInstanceWithoutAuth
* @returns {AxiosInstance} κΈ°λ³Έ μ„€μ •λ§Œ 된 Axios μΈμŠ€ν„΄μŠ€
*
* @description
* 이 ν•¨μˆ˜λŠ” κΈ°λ³Έ URL만 μ„€μ •λœ Axios μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
* 인증 토큰이 ν•„μš”ν•˜μ§€ μ•Šμ€ API μš”μ²­(예: 둜그인, νšŒμ›κ°€μž… λ“±)에 μ‚¬μš©λ©λ‹ˆλ‹€.
*/
// Todo: .env νŒŒμΌμ—μ„œ ν™˜κ²½λ³€μˆ˜λ‘œ api μ£Όμ†Œλ₯Ό κ°€μ Έμ˜€κ³  μžˆμ§€λ§Œ, μ„œλ²„ μ£Όμ†Œκ°€ λ‚˜μ˜€λ©΄ env 파일 없이도 μ‚¬μš© κ°€λŠ₯ν•˜κ²Œ μˆ˜μ •ν•΄μ•Ό 함.
function createInstanceWithoutAuth() {
const instance = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
});
return instance;
}

/**
* 인증이 ν•„μš”ν•œ API μš”μ²­μ— μ‚¬μš©ν•  Axios μΈμŠ€ν„΄μŠ€
* @const {AxiosInstance}
*/
export const api = createInstance();

/**
* 인증이 ν•„μš”ν•˜μ§€ μ•Šμ€ API μš”μ²­μ— μ‚¬μš©ν•  Axios μΈμŠ€ν„΄μŠ€
* @const {AxiosInstance}
*/
export const apiWithoutAuth = createInstanceWithoutAuth();
23 changes: 23 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const setAccessToken = (token: string | null) => {
localStorage.setItem('authorization', token || '');
};

/**
* 둜컬 μŠ€ν† λ¦¬μ§€μ—μ„œ 인증 토큰을 κ°€μ Έμ˜€λŠ” ν•¨μˆ˜
*
* @function getAccessToken
* @returns {string | null} μ €μž₯된 인증 토큰 λ˜λŠ” null
*
* @description
* 이 ν•¨μˆ˜λŠ” 둜컬 μŠ€ν† λ¦¬μ§€μ—μ„œ 'authorization' ν‚€λ‘œ μ €μž₯된 인증 토큰을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
* 토큰이 μ—†λŠ” 경우 null을 λ°˜ν™˜ν•©λ‹ˆλ‹€.
*/
// @utils/auth.ts
export const getAccessToken = () => {
return localStorage.getItem('authorization');
};


export const deleteAccessToken = () => {
localStorage.removeItem('authorization');
};

0 comments on commit 5cbb712

Please sign in to comment.