Skip to content

Commit

Permalink
πŸ“ style: 토큰 κ΄€λ ¨ 였λ₯˜ λ©”μ‹œμ§€ μƒμˆ˜ν™”
Browse files Browse the repository at this point in the history
  • Loading branch information
bbearcookie committed Feb 6, 2024
1 parent d80ce41 commit caea6a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ it('AT 유효', async () => {
localStorage.setItem(STORAGE_KEYS.accessToken, 'fresh');
localStorage.setItem(STORAGE_KEYS.refreshToken, 'fresh');

expect(authAPI.getAccessCheck()).resolves.toBe('μ•‘μ„ΈμŠ€ 토큰이 μœ νš¨ν•©λ‹ˆλ‹€.');
expect(authAPI.getAccessCheck()).resolves.not.toThrow();
});

it('AT λ§Œλ£Œμ‹œ μž¬λ°œκΈ‰ μš”μ²­ ν›„ 원본 μš”μ²­ μž¬μˆ˜ν–‰', async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/api/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { isAxiosError } from 'axios';
import { BACKEND_ENDPOINT } from '@/constants/endpoint';
import STORAGE_KEYS from '@/constants/storageKeys';
import { ROUTER_PATHS } from '@/router';
import ERROR_RESPONSES from '@/constants/errorMessages';
import authAPI from './auth/apis';

const baseInstance = axios.create({
Expand All @@ -25,13 +26,13 @@ baseInstance.interceptors.response.use(

if (isAxiosError(error)) {
switch (error.response?.data) {
case 'μ•‘μ„ΈμŠ€ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.': {
case ERROR_RESPONSES.accessExpired: {
const res = await authAPI.getReissue();
localStorage.setItem(STORAGE_KEYS.accessToken, res.accessToken);
localStorage.setItem(STORAGE_KEYS.refreshToken, res.refreshToken);
return axios(config);
}
case 'λ¦¬ν”„λ ˆμ‹œ 토큰이 μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•΄μ£Όμ„Έμš”.': {
case ERROR_RESPONSES.reissueFailed: {
localStorage.removeItem(STORAGE_KEYS.accessToken);
localStorage.removeItem(STORAGE_KEYS.refreshToken);
window.location.href = ROUTER_PATHS.SIGNIN;
Expand Down
6 changes: 6 additions & 0 deletions src/constants/errorMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const ERROR_RESPONSES = {
accessExpired: 'μ•‘μ„ΈμŠ€ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.',
reissueFailed: 'λ¦¬ν”„λ ˆμ‹œ 토큰이 μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•΄μ£Όμ„Έμš”.',
} as const;

export default ERROR_RESPONSES;
8 changes: 3 additions & 5 deletions src/mocks/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { http, HttpResponse, delay } from 'msw';
import { baseURL } from '@/utils/mswUtils';
import ERROR_RESPONSES from '@/constants/errorMessages';

const authHandler = [
http.post(baseURL('/api/oauth2/authorization/kakao'), async () => {
Expand All @@ -19,10 +20,7 @@ const authHandler = [
refreshToken: 'renewed',
});
} else {
return new HttpResponse(
'λ¦¬ν”„λ ˆμ‹œ 토큰이 μ˜¬λ°”λ₯΄μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€μ‹œ λ‘œκ·ΈμΈν•΄μ£Όμ„Έμš”.',
{ status: 401 },
);
return new HttpResponse(ERROR_RESPONSES.reissueFailed, { status: 401 });
}
}),

Expand All @@ -33,7 +31,7 @@ const authHandler = [
if (accessToken === 'fresh') {
return new HttpResponse('μ•‘μ„ΈμŠ€ 토큰이 μœ νš¨ν•©λ‹ˆλ‹€.');
} else {
return new HttpResponse('μ•‘μ„ΈμŠ€ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.', {
return new HttpResponse(ERROR_RESPONSES.accessExpired, {
status: 401,
});
}
Expand Down

0 comments on commit caea6a1

Please sign in to comment.