@@ -3,61 +3,78 @@ import { useMutation } from "@tanstack/react-query";
33
44import AUTH_ENDPOINTS from "../Constant/Auth.Endpoint.Constant" ;
55import 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+ } ;
0 commit comments