11import axios , { CreateAxiosDefaults , InternalAxiosRequestConfig } from 'axios' ;
22
33import { AuthRefreshIgnoredError } from '@/types/customError' ;
4- import { ACCESS_TOKEN_STORAGE_KEY , SERVICE_ERROR_MESSAGE } from '@/constants' ;
4+ import { SERVICE_ERROR_MESSAGE , SESSION_COOKIES_KEYS } from '@/constants' ;
55import {
66 isAuthFailedError ,
77 isAuthRefreshError ,
88 isAxiosErrorWithCustomCode ,
99} from '@/utils/helpers' ;
10- import webStorage from '@/utils/storage' ;
10+ import { deleteAuthSession , setAuthSession } from '@/server/session' ;
11+ import { deleteCookie } from '@/utils/cookie' ;
1112
12- const storage = webStorage ( ACCESS_TOKEN_STORAGE_KEY ) ;
1313const options : CreateAxiosDefaults = {
1414 baseURL : process . env . NEXT_HOST ,
1515 headers : {
@@ -25,11 +25,6 @@ export const publicApi = axios.create({
2525
2626const requestHandler = ( config : InternalAxiosRequestConfig ) => {
2727 const { data, method } = config ;
28- const accessToken = storage . get ( ) ;
29-
30- if ( accessToken ) {
31- setAxiosAuthHeader ( config , accessToken ) ;
32- }
3328
3429 if ( ! data && ( method === 'get' || method === 'delete' ) ) {
3530 config . data = { } ;
@@ -51,7 +46,7 @@ const responseHandler = async (error: unknown) => {
5146 }
5247
5348 if ( isAuthFailedError ( code ) ) {
54- removeToken ( ) ;
49+ await removeToken ( ) ;
5550 }
5651 } else {
5752 console . error ( '예상하지 못한 오류가 발생했어요.\n' , error ) ;
@@ -63,12 +58,10 @@ const responseHandler = async (error: unknown) => {
6358const silentRefresh = async ( originRequest : InternalAxiosRequestConfig ) => {
6459 try {
6560 const newToken = await updateToken ( ) ;
66- storage . set ( newToken ) ;
67- setAxiosAuthHeader ( originRequest , newToken ) ;
68-
61+ await setAuthSession ( newToken ) ;
6962 return await publicApi ( originRequest ) ;
7063 } catch ( error ) {
71- removeToken ( ) ;
64+ await removeToken ( ) ;
7265 return Promise . reject ( error ) ;
7366 }
7467} ;
@@ -93,15 +86,9 @@ const updateToken = () =>
9386 . finally ( ( ) => ( isTokenRefreshing = false ) ) ;
9487 } ) ;
9588
96- const removeToken = ( ) => {
97- storage . remove ( ) ;
98- } ;
99-
100- const setAxiosAuthHeader = (
101- config : InternalAxiosRequestConfig ,
102- token : string
103- ) => {
104- config . headers [ 'Authorization' ] = `Bearers ${ token } ` ;
89+ const removeToken = async ( ) => {
90+ SESSION_COOKIES_KEYS . map ( key => deleteCookie ( key ) ) ;
91+ await deleteAuthSession ( ) ;
10592} ;
10693
10794publicApi . interceptors . request . use ( requestHandler ) ;
0 commit comments