-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthStore.ts
82 lines (76 loc) · 2.63 KB
/
authStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { UserStatus, type UserStatusType } from '@models/user';
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { useDemoStore } from './demoStore';
import { initDemoState } from './services/demoService';
import {
ACCESS_TOKEN_NAME,
AUTH_STORE_NAME,
CURRENT_VERSION,
DEMO_STORE_NAME,
} from './storeConfig';
type AuthStoreType = {
userStatus: UserStatusType;
setLoginState: () => void;
setLogoutState: () => void;
setDemoState: () => void;
};
export const useAuthStore = create(
persist<AuthStoreType>(
(set) => ({
userStatus: UserStatus.LoggedOut,
setLoginState: () =>
set(() => ({
userStatus: UserStatus.LoggedIn,
})),
setLogoutState: () => {
// 로컬스토리지 access_token 삭제
window.localStorage.removeItem(ACCESS_TOKEN_NAME);
// 로컬스토리지 Demo Data 삭제
window.localStorage.setItem(DEMO_STORE_NAME, JSON.stringify(initDemoState()));
// DemoStore 초기화
useDemoStore.getState().initDemoExpenses();
set(() => ({
userStatus: UserStatus.LoggedOut,
}));
},
setDemoState: () => {
// 필요하면 상태 로컬스토리지에 저장하기
// window.localStorage.setItem('demo_mode', 'true');
set(() => ({
userStatus: UserStatus.Demo,
}));
},
}),
{
name: AUTH_STORE_NAME,
storage: createJSONStorage(() => localStorage),
version: CURRENT_VERSION,
migrate: (persistedState, version) => {
if (version === undefined || version < CURRENT_VERSION) {
// 버전이 없거나 현재 버전(CURRENT_VERSION)보다 낮은 경우
window.localStorage.removeItem(ACCESS_TOKEN_NAME);
}
return persistedState as AuthStoreType; // 상태를 변경하지 않고 그대로 반환
},
},
),
);
// 로컬스토리지에 이전에 저장한 토큰이 존재한다면, login state
const initializeUser = () => {
//#20241117.syjang, 무조건 데모 모드로 넘어가게 세팅 (최적화 측정용)
// useAuthStore.getState().setDemoState();
if (import.meta.env.MODE === 'development') {
// demo mode 개발로 주석처리
//useAuthStore.getState().setLoginState();
} else {
// const accessToken = window.localStorage.getItem(ACCESS_TOKEN_NAME);
// if (accessToken) {
// useAuthStore.getState().setLoginState();
// } else {
// // 토큰 없으면 서비스 오픈 시 무조건 로그아웃 스테이트가 맞음
// useAuthStore.getState().setLogoutState();
// }
}
};
initializeUser();