34 lines
851 B
TypeScript
34 lines
851 B
TypeScript
![]() |
import { User } from "@root/model/auth";
|
||
|
import { create } from "zustand";
|
||
|
import { createJSONStorage, devtools, persist } from "zustand/middleware";
|
||
|
|
||
|
|
||
|
interface UserStore {
|
||
|
userId: string | null;
|
||
|
user: User | null;
|
||
|
}
|
||
|
|
||
|
const initialState: UserStore = {
|
||
|
userId: null,
|
||
|
user: null,
|
||
|
};
|
||
|
|
||
|
export const useUserStore = create<UserStore>()(
|
||
|
persist(
|
||
|
devtools(
|
||
|
(set, get) => initialState,
|
||
|
{
|
||
|
name: "User store",
|
||
|
}
|
||
|
),
|
||
|
{
|
||
|
name: "user",
|
||
|
storage: createJSONStorage(() => localStorage),
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
|
||
|
export const setUserId = (userId: string | null) => useUserStore.setState({ userId });
|
||
|
export const setUser = (user: User | null) => useUserStore.setState({ user });
|
||
|
|
||
|
export const clearUser = () => useUserStore.setState({ ...initialState });
|