-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthStore.ts
37 lines (35 loc) · 868 Bytes
/
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
import axios from "axios";
import create from "zustand";
import { devtools, persist } from "zustand/middleware";
import { IUser } from "../types";
import { BASE_URL } from "../utils/constants";
interface AuthState {
userProfile: any;
allUsers: Array<IUser>;
addUser: (user: any) => void;
removeUser: () => void;
fetchAllUsers: () => void;
}
const useAuthStore = create<AuthState>()(
devtools(
persist(
set => ({
userProfile: null,
allUsers: [],
addUser: user => set({ userProfile: user }),
removeUser: () => set({ userProfile: null }),
fetchAllUsers: async () => {
try {
const { data }: { data: Array<IUser> } =
await axios.get(`${BASE_URL}/api/users`);
set({ allUsers: data });
} catch (error) {
set({ allUsers: [] });
}
},
}),
{ name: "auth" }
)
)
);
export default useAuthStore;