Spaces:
Sleeping
Sleeping
| import { create } from "zustand"; | |
| import { persist, createJSONStorage } from "zustand/middleware"; | |
| import type { UserInfo } from "@/types"; | |
| import { useChatStore } from "@/stores/chatStore"; | |
| interface AuthState { | |
| userInfo: UserInfo | null; | |
| isLoggedIn: boolean; | |
| login: (userInfo: UserInfo) => void; | |
| logout: () => void; | |
| } | |
| export const useAuthStore = create<AuthState>()( | |
| persist( | |
| (set) => ({ | |
| userInfo: null, | |
| isLoggedIn: false, | |
| login: (userInfo: UserInfo) => { | |
| set({ userInfo, isLoggedIn: true }); | |
| useChatStore.getState().hydrate(userInfo.user_id); | |
| }, | |
| logout: () => { | |
| useChatStore.getState().reset(); | |
| set({ userInfo: null, isLoggedIn: false }); | |
| }, | |
| }), | |
| { | |
| name: "ema-auth", | |
| storage: createJSONStorage(() => localStorage), | |
| partialize: (state) => ({ | |
| userInfo: state.userInfo, | |
| isLoggedIn: state.isLoggedIn, | |
| }), | |
| } | |
| ) | |
| ); | |