Skip to content

Commit 5736a31

Browse files
feat: add organization management with new store and update authentication flow
1 parent 4ec1759 commit 5736a31

8 files changed

Lines changed: 131 additions & 66 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const AUTH_ENDPOINTS = {
22
LOGIN: "/signIn",
33
REGISTER: "/auth/register",
44
REFRESH: "/auth/refresh",
5-
GET_ORGANIZATION_BY_ID: "/organization",
5+
GET_ORGANIZATION_BY_ID: "/get-community-by-auth-id",
66
};
77

88
export default AUTH_ENDPOINTS;

src/features/Auth/v1/Store/Auth.Store.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
11
import { create } from "zustand";
22
import { persist } from "zustand/middleware";
33

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-
}
4+
import { AuthState, User } from "../Types/Auth.type";
195

206
const useAuthStore = create<AuthState>()(
217
persist(
228
(set) => ({
239
token: null,
2410
user: null,
2511

26-
setAuthData: (user) =>
12+
setAuthData: (user: User) =>
2713
set({
2814
user,
2915
}),
3016

3117
clearAuthData: () =>
3218
set({
33-
3419
user: null,
3520
}),
3621
}),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { create } from "zustand";
2+
import { persist } from "zustand/middleware";
3+
import { CommunitySchema } from "../Types/Organization.Type";
4+
5+
6+
const useOrganizationStore = create<{
7+
organization: CommunitySchema | null;
8+
setOrganization: (organization: CommunitySchema) => void;
9+
}>()(
10+
persist(
11+
(set) => ({
12+
organization: null,
13+
setOrganization: (organization: CommunitySchema) =>
14+
set({
15+
organization,
16+
}),
17+
}),
18+
{
19+
name: "organization-storage",
20+
},
21+
),
22+
);
23+
24+
export default useOrganizationStore;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface User {
2+
_id: string;
3+
4+
email: string;
5+
role: string;
6+
}
7+
8+
export interface AuthState {
9+
// token: string | null;
10+
user: User | null;
11+
12+
setAuthData: (user: User) => void;
13+
clearAuthData: () => void;
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type CommunitySchema = {
2+
CommunityName: string;
3+
Bio: string;
4+
Slug: string;
5+
Website: string;
6+
Country: string;
7+
City: string;
8+
OfficialEmail: string;
9+
ContactPhone: string;
10+
LogoUrl: string;
11+
12+
SocialLinks: {
13+
github?: string;
14+
discord?: string;
15+
twitter?: string;
16+
linkedin?: string;
17+
youtube?: string;
18+
instagram?: string;
19+
};
20+
};

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

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,78 @@ import { useMutation } from "@tanstack/react-query";
33

44
import AUTH_ENDPOINTS from "../Constant/Auth.Endpoint.Constant";
55
import useAuthStore from "../Store/Auth.Store";
6+
import useOrganizationStore from "../Store/Organization.Store";
67

7-
const baseUrl = import.meta.env.VITE_API_BASE_URL || "http://localhost:8000/api/v1";
8+
const baseUrl =
9+
import.meta.env.VITE_API_BASE_URL || "http://localhost:8000/api/v1";
810

911
// =========================
10-
// LOGIN MUTATION HOOK
12+
// GET ORGANIZATION
1113
// =========================
1214

13-
const useLoginMutation = () => {
15+
const useGetOrganizationMutation = () => {
1416
return useMutation({
15-
mutationKey: ["login"],
16-
17-
mutationFn: async (credentials: { email: string; password: string }) => {
18-
try {
19-
const response = await api.post(`${baseUrl}${AUTH_ENDPOINTS.LOGIN}`, credentials);
20-
return response.data;
21-
} catch (error) {
22-
console.error("Login failed:", error);
23-
throw error;
24-
}
25-
},
17+
mutationKey: ["organization"],
2618

19+
mutationFn: async (_id: string) => {
20+
const response = await api.get(
21+
`${baseUrl}${AUTH_ENDPOINTS.GET_ORGANIZATION_BY_ID}?ownerId=${_id}`
22+
);
2723

24+
return response.data;
25+
},
2826

2927
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-
}
28+
useOrganizationStore.getState().setOrganization(response.data);
29+
},
3930

40-
useAuthStore.getState().setAuthData(user);
31+
onError: (error) => {
32+
console.error("Failed to fetch organization:", error);
4133
},
4234
});
4335
};
4436

37+
// =========================
38+
// LOGIN
39+
// =========================
40+
41+
const useLoginMutation = () => {
42+
const organizationMutation = useGetOrganizationMutation();
4543

46-
const useGetOrganization_Mutation = () => {
4744
return useMutation({
48-
mutationKey: ["organization"],
45+
mutationKey: ["login"],
46+
47+
mutationFn: async (credentials: {
48+
email: string;
49+
password: string;
50+
}) => {
51+
const response = await api.post(
52+
`${baseUrl}${AUTH_ENDPOINTS.LOGIN}`,
53+
credentials
54+
);
55+
56+
return response.data;
57+
},
58+
59+
onSuccess: async (response) => {
60+
const user = response.data;
4961

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;
62+
console.log("Login successful:", user);
63+
64+
// Save auth first
65+
useAuthStore.getState().setAuthData(user);
66+
67+
// Fetch organization if needed
68+
if (user.role === "organization") {
69+
await organizationMutation.mutateAsync(user._id);
5770
}
5871
},
72+
73+
onError: (error) => {
74+
console.error("Login failed:", error);
75+
},
5976
});
60-
}
77+
};
6178

6279
// =========================
6380
// AUTH HOOK
@@ -69,4 +86,4 @@ export const useAuth = () => {
6986
return {
7087
loginMutation,
7188
};
72-
};
89+
};

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import SideBarLink from "../Components/SideBarLink";
1515
import { dashboardData } from "@/features/Member/v1/mock/dashboardData";
1616
import useAuthStore from "@/features/Auth/v1/Store/Auth.Store";
1717
import { useEffect } from "react";
18+
import useOrganizationStore from "@/features/Auth/v1/Store/Organization.Store";
1819

1920
const SideBar = () => {
2021
const user = useAuthStore((state) => state.user);
21-
const { theme } = useTheme();
22-
22+
const organization = useOrganizationStore((state) => state.organization);
2323

24+
console.log("User in SideBar:", user);
25+
console.log("Organization in SideBar:", organization);
26+
const { theme } = useTheme();
2427

2528
return (
2629
<div
@@ -80,13 +83,13 @@ const SideBar = () => {
8083
style={{ backgroundColor: theme.bg.surfaceSecondary }}
8184
>
8285
<img
83-
src="https://randomuser.me/api/portraits/men/1.jpg"
86+
src={organization?.LogoUrl || "/defaultProfile.png"}
8487
alt="Profile"
8588
className="w-9 h-9 rounded-full object-cover shrink-0"
8689
/>
8790
<div className="min-w-0 flex-1 flex flex-col justify-center gap-0.5">
8891
<p className="text-sm font-semibold truncate" style={{ color: theme.text.primary }}>
89-
{/* {data?.user?.name} */}
92+
{organization?.CommunityName}
9093
</p>
9194
<p className="text-xs truncate font-medium" style={{ color: theme.primary.default }}>
9295
{user?.role}

src/features/template/LoginUserTemplate.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
import useAuthStore from "../Auth/v1/Store/Auth.Store";
12
import SideBar from "../SideBar/v1/Section/SideBar";
23
import { Outlet, redirect } from "react-router";
34
import BotamNavBar from "../SideBar/v1/Section/BotamNavBar";
4-
import { useAuth } from "../Auth/v1/hooks/useAuth";
5+
56
import { useMemo } from "react";
67

78
const Organisation_Template = () => {
8-
const { loginMutation } = useAuth();
9+
10+
let user = useAuthStore((state) => state.user);
911

1012
useMemo(() => {
11-
let { data } = loginMutation;
1213

13-
if (data) {
14-
let Role = data.role;
14+
console.log("User in Organisation_Template-->:", user);
15+
1516

16-
if (Role === "org") {
17-
redirect("/");
17+
if (user?.role) {
18+
if (user.role !== "organization") {
19+
redirect("/");
20+
}
1821
}
19-
}
20-
}, [loginMutation]);
22+
}, [user]);
2123

2224
return (
2325
<div className="flex w-screen min-h-screen overflow-x-hidden items-stretch cd-page">

0 commit comments

Comments
 (0)