Refactor: api, 세션 스토리지 기준으로 변경

This commit is contained in:
정현조 2024-09-12 10:54:21 +09:00
parent 4aa1dee1e9
commit 07cbe72ba9
2 changed files with 8 additions and 14 deletions

View File

@ -30,7 +30,7 @@ const processQueue = (error: Error | null, token: string | undefined = undefined
}; };
api.interceptors.request.use((config: InternalAxiosRequestConfig) => { api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
const token = localStorage.getItem('accessToken'); const token = sessionStorage.getItem('accessToken');
if (token && config.headers) { if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`; config.headers.Authorization = `Bearer ${token}`;
} }
@ -72,7 +72,7 @@ api.interceptors.response.use(
} }
useAuthStore.getState().setLoggedIn(true, newAccessToken); useAuthStore.getState().setLoggedIn(true, newAccessToken);
localStorage.setItem('accessToken', newAccessToken); sessionStorage.setItem('accessToken', newAccessToken);
processQueue(null, newAccessToken); processQueue(null, newAccessToken);
if (originalRequest.headers) { if (originalRequest.headers) {

View File

@ -1,18 +1,13 @@
import { create } from 'zustand'; import { create } from 'zustand';
import { persist } from 'zustand/middleware'; import { persist } from 'zustand/middleware';
import { MemberResponseDTO } from '@/types';
interface ProfileData {
id: number | null;
nickname: string;
profileImage: string;
}
interface AuthState { interface AuthState {
isLoggedIn: boolean; isLoggedIn: boolean;
accessToken: string; accessToken: string;
profile: ProfileData; profile: MemberResponseDTO | null;
setLoggedIn: (status: boolean, token: string) => void; setLoggedIn: (status: boolean, token: string) => void;
setProfile: (profile: ProfileData) => void; setProfile: (profile: MemberResponseDTO) => void;
clearAuth: () => void; clearAuth: () => void;
} }
@ -21,15 +16,14 @@ const useAuthStore = create<AuthState>()(
(set) => ({ (set) => ({
isLoggedIn: false, isLoggedIn: false,
accessToken: '', accessToken: '',
profile: { id: null, nickname: '', profileImage: '' }, profile: null,
setLoggedIn: (status, token) => set({ isLoggedIn: status, accessToken: token }), setLoggedIn: (status, token) => set({ isLoggedIn: status, accessToken: token }),
setProfile: (profile) => set({ profile }), setProfile: (profile) => set({ profile }),
clearAuth: () => clearAuth: () => set({ isLoggedIn: false, accessToken: '', profile: null }),
set({ isLoggedIn: false, accessToken: '', profile: { id: null, nickname: '', profileImage: '' } }),
}), }),
{ {
name: 'auth-storage', name: 'auth-storage',
getStorage: () => localStorage, getStorage: () => sessionStorage,
} }
) )
); );