55 lines
1 KiB
JavaScript
55 lines
1 KiB
JavaScript
|
|
import { create } from 'zustand';
|
||
|
|
import { persist } from 'zustand/middleware';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 인증 상태 스토어
|
||
|
|
* localStorage에 지속되어 새로고침 후에도 유지
|
||
|
|
*/
|
||
|
|
const useAuthStore = create(
|
||
|
|
persist(
|
||
|
|
(set, get) => ({
|
||
|
|
// 상태
|
||
|
|
token: null,
|
||
|
|
user: null,
|
||
|
|
isAuthenticated: false,
|
||
|
|
|
||
|
|
// 로그인
|
||
|
|
login: (token, user) => {
|
||
|
|
set({
|
||
|
|
token,
|
||
|
|
user,
|
||
|
|
isAuthenticated: true,
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
// 로그아웃
|
||
|
|
logout: () => {
|
||
|
|
set({
|
||
|
|
token: null,
|
||
|
|
user: null,
|
||
|
|
isAuthenticated: false,
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
// 토큰 가져오기
|
||
|
|
getToken: () => get().token,
|
||
|
|
|
||
|
|
// 인증 여부 확인
|
||
|
|
checkAuth: () => {
|
||
|
|
const { token } = get();
|
||
|
|
return !!token;
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
{
|
||
|
|
name: 'auth-storage',
|
||
|
|
partialize: (state) => ({
|
||
|
|
token: state.token,
|
||
|
|
user: state.user,
|
||
|
|
isAuthenticated: state.isAuthenticated,
|
||
|
|
}),
|
||
|
|
}
|
||
|
|
)
|
||
|
|
);
|
||
|
|
|
||
|
|
export default useAuthStore;
|