feat: userType, JWT 관리용 zustand store 추가

This commit is contained in:
jhynsoo 2024-07-21 00:14:58 +09:00
parent bb08f334fe
commit e47803fa71
3 changed files with 16 additions and 4 deletions

View File

@ -0,0 +1,4 @@
export const tokenSlice = (set) => ({
token: null,
setToken: (token) => set({ token }),
});

View File

@ -0,0 +1,9 @@
// userType : null, 'teacher', 'student'
export const userTypeSlice = (set) => ({
userType: null,
setUserType: (userType) => set({ userType }),
isTeacher: () => set.userType === 'teacher',
isStudent: () => set.userType === 'student',
isGuest: () => set.userType === null,
});

View File

@ -1,6 +1,5 @@
import axios from 'axios';
const ACCESS_TOKEN_KEY = import.meta.env.VITE_ACCESS_TOKEN_KEY;
import useBoundStore from '../../store';
const instance = axios.create({
baseURL: import.meta.env.VITE_API_URL,
@ -9,7 +8,7 @@ const instance = axios.create({
});
instance.interceptors.request.use((config) => {
const accessToken = sessionStorage.getItem(ACCESS_TOKEN_KEY);
const accessToken = useBoundStore.getState().accessToken;
if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
@ -31,7 +30,7 @@ instance.interceptors.response.use(
return instance.post(REFRESH_API_URL).then((response) => {
const { accessToken } = response.data;
sessionStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
useBoundStore.setState({ accessToken });
error.config.headers.Authorization = `Bearer ${accessToken}`;
return instance(error.config);
});