Skip to content

Commit

Permalink
chore: 토큰 유무 로직 추가 및 파일 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
21ow committed Dec 19, 2024
1 parent fa0735e commit 7af47d9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/_api/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios';
import NO_TOKEN_ENDPOINTS from './constant/noTokenEndPoints';
import isAuthRequired from './util/isAuthRequired';
import { getAccessToken, _LOGIN_NEED_MESSAGE_ } from './storage/authStorage';

const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL;
Expand All @@ -12,16 +14,22 @@ const axiosInstance = axios.create({
},
});

const NO_TOKEN_ENDPOINTS = ['/auth/signUp', '/auth/signIn', '/oauthApps'];

axiosInstance.interceptors.request.use(
(config) => {
const token = getAccessToken();
//[email protected], 12341234
//임시 토큰
// const token =
// 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NzMyLCJ0ZWFtSWQiOiIxMC0zIiwiaWF0IjoxNzM0NTEzNTUyLCJpc3MiOiJzcC1tb2dhem9hIn0.tbvOoiH-iTR1CZW5rhjJW5KSr2Go8cxGwqRWxLGwqZ8';

const path = config.url || '';
const method = config?.method || '';

if (!isAuthRequired(path, method)) {
return config;
}

const token = getAccessToken();

if (!token && config.url && !NO_TOKEN_ENDPOINTS.includes(config.url)) {
throw new Error(`Failed to getAccessToken(), ${_LOGIN_NEED_MESSAGE_}`);
}
Expand Down
9 changes: 9 additions & 0 deletions src/_api/constant/noTokenEndPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const NO_TOKEN_ENDPOINTS = [
'/auth/',
'/categories',
'/oauthApps',
'/products',
`/users/`,
];

export default NO_TOKEN_ENDPOINTS;
19 changes: 19 additions & 0 deletions src/_api/util/isAuthRequired.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import NO_TOKEN_ENDPOINTS from '../constant/noTokenEndPoints';

const isAuthRequired = (path: string, method: string): boolean => {
if (path === '/users/me') {
return true;
}

if (path.startsWith('/users/')) {
return false;
}

if (method === 'POST' && path.startsWith('/products')) {
return true;
}

return !NO_TOKEN_ENDPOINTS.some((endpoint) => path.startsWith(endpoint));
};

export default isAuthRequired;

0 comments on commit 7af47d9

Please sign in to comment.