feat: userType, JWT 관리용 zustand store 추가
This commit is contained in:
parent
bb08f334fe
commit
e47803fa71
4
frontend/src/store/tokenSlice.js
Normal file
4
frontend/src/store/tokenSlice.js
Normal file
@ -0,0 +1,4 @@
|
||||
export const tokenSlice = (set) => ({
|
||||
token: null,
|
||||
setToken: (token) => set({ token }),
|
||||
});
|
9
frontend/src/store/userTypeSlice.js
Normal file
9
frontend/src/store/userTypeSlice.js
Normal 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,
|
||||
});
|
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user