Skip to content

Commit 4ec1759

Browse files
feat: implement authentication store and add organization fetching logic
1 parent 3efd561 commit 4ec1759

6 files changed

Lines changed: 116 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"react-router-dom": "^7.15.0",
4040
"recharts": "^3.8.1",
4141
"tailwind-merge": "^3.5.0",
42-
"zod": "^4.4.3"
42+
"zod": "^4.4.3",
43+
"zustand": "^5.0.13"
4344
},
4445
"devDependencies": {
4546
"@eslint/js": "^9.39.4",

pnpm-lock.yaml

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/Auth/v1/Constant/Auth.Endpoint.Constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const AUTH_ENDPOINTS = {
22
LOGIN: "/signIn",
33
REGISTER: "/auth/register",
44
REFRESH: "/auth/refresh",
5+
GET_ORGANIZATION_BY_ID: "/organization",
56
};
67

78
export default AUTH_ENDPOINTS;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { create } from "zustand";
2+
import { persist } from "zustand/middleware";
3+
4+
export interface User {
5+
_id: string;
6+
7+
email: string;
8+
role: string;
9+
10+
}
11+
12+
interface AuthState {
13+
// token: string | null;
14+
user: User | null;
15+
16+
setAuthData: (user: User) => void;
17+
clearAuthData: () => void;
18+
}
19+
20+
const useAuthStore = create<AuthState>()(
21+
persist(
22+
(set) => ({
23+
token: null,
24+
user: null,
25+
26+
setAuthData: (user) =>
27+
set({
28+
user,
29+
}),
30+
31+
clearAuthData: () =>
32+
set({
33+
34+
user: null,
35+
}),
36+
}),
37+
{
38+
name: "auth-storage",
39+
},
40+
),
41+
);
42+
43+
export default useAuthStore;

src/features/Auth/v1/hooks/useAuth.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import api from "@/utils/axios.utils";
22
import { useMutation } from "@tanstack/react-query";
3+
34
import AUTH_ENDPOINTS from "../Constant/Auth.Endpoint.Constant";
5+
import useAuthStore from "../Store/Auth.Store";
46

57
const baseUrl = import.meta.env.VITE_API_BASE_URL || "http://localhost:8000/api/v1";
68

@@ -14,17 +16,49 @@ const useLoginMutation = () => {
1416

1517
mutationFn: async (credentials: { email: string; password: string }) => {
1618
try {
17-
console.log("Attempting login with credentials:", credentials);
1819
const response = await api.post(`${baseUrl}${AUTH_ENDPOINTS.LOGIN}`, credentials);
1920
return response.data;
2021
} catch (error) {
2122
console.error("Login failed:", error);
2223
throw error;
2324
}
2425
},
26+
27+
28+
29+
onSuccess: (response) => {
30+
31+
const user = response.data;
32+
console.log("Login successful:", user);
33+
34+
if(user.role === "organization"){
35+
let {mutate} = useGetOrganization_Mutation()
36+
mutate(user._id)
37+
38+
}
39+
40+
useAuthStore.getState().setAuthData(user);
41+
},
2542
});
2643
};
2744

45+
46+
const useGetOrganization_Mutation = () => {
47+
return useMutation({
48+
mutationKey: ["organization"],
49+
50+
mutationFn: async (id: string) => {
51+
try {
52+
const response = await api.get(`${baseUrl}${AUTH_ENDPOINTS.GET_ORGANIZATION_BY_ID}?id=${id}`);
53+
return response.data;
54+
} catch (error) {
55+
console.error("Failed to fetch organization data:", error);
56+
throw error;
57+
}
58+
},
59+
});
60+
}
61+
2862
// =========================
2963
// AUTH HOOK
3064
// =========================

src/features/SideBar/v1/Section/SideBar.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import { ThemeToggle } from "@/Component/ui/ThemeToggle";
1313

1414
import SideBarLink from "../Components/SideBarLink";
1515
import { dashboardData } from "@/features/Member/v1/mock/dashboardData";
16+
import useAuthStore from "@/features/Auth/v1/Store/Auth.Store";
17+
import { useEffect } from "react";
1618

1719
const SideBar = () => {
20+
const user = useAuthStore((state) => state.user);
1821
const { theme } = useTheme();
1922

23+
24+
2025
return (
2126
<div
2227
className="w-[25%] 2xl:w-[18%] min-h-screen hidden lg:flex flex-col"
@@ -81,10 +86,10 @@ const SideBar = () => {
8186
/>
8287
<div className="min-w-0 flex-1 flex flex-col justify-center gap-0.5">
8388
<p className="text-sm font-semibold truncate" style={{ color: theme.text.primary }}>
84-
{dashboardData.user.name}
89+
{/* {data?.user?.name} */}
8590
</p>
8691
<p className="text-xs truncate font-medium" style={{ color: theme.primary.default }}>
87-
{dashboardData.user.role}
92+
{user?.role}
8893
</p>
8994
</div>
9095
</div>

0 commit comments

Comments
 (0)