-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthStore.ts
More file actions
52 lines (47 loc) Β· 1.4 KB
/
authStore.ts
File metadata and controls
52 lines (47 loc) Β· 1.4 KB
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
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import type { AuthState } from '@/features/auth/types/auth'
import type { ProfileResponse } from '@/features/profile/types/profile'
export const useAuthStore = create<AuthState>()(
persist(
(set) => ({
userInfo: { userId: '', username: '', userProfileImageUrl: null },
isLogin: false,
accessToken: '',
setLogin: (response) => {
sessionStorage.removeItem('anonymous_token')
const { userId, username, userProfileImageUrl, jwtAccessToken } = response
set({
userInfo: { userId, username, userProfileImageUrl },
accessToken: jwtAccessToken,
isLogin: true,
})
},
setLogout: () => {
localStorage.removeItem('deulak_auth')
set({
userInfo: { userId: '', username: '', userProfileImageUrl: null },
accessToken: '',
isLogin: false,
})
},
updateUserInfo: (payload: ProfileResponse) => {
set({
userInfo: {
userId: payload.userId,
username: payload.nickname,
userProfileImageUrl: payload.profileImageUrl,
},
})
},
}),
{
name: 'deulak_auth',
partialize: (state) => ({
userInfo: state.userInfo,
accessToken: state.accessToken,
isLogin: state.isLogin,
}),
}
)
)