11import { useState , useCallback } from 'react' ;
22import { supabase } from '@/integrations/supabase/client' ;
33import { toast } from '@/hooks/use-toast' ;
4+ import { useAuth } from '@/contexts/AuthContext' ;
45
56export interface ShopifyIntegration {
67 id : string ;
@@ -25,17 +26,18 @@ export interface ShopifyIntegration {
2526export const useShopifySupabase = ( campaignId : string ) => {
2627 const [ integrations , setIntegrations ] = useState < ShopifyIntegration [ ] > ( [ ] ) ;
2728 const [ isLoading , setIsLoading ] = useState ( false ) ;
29+ const { user } = useAuth ( ) ;
2830
2931 // Récupérer les intégrations existantes
3032 const fetchIntegrations = useCallback ( async ( ) => {
3133 try {
32- const { data : { user } } = await supabase . auth . getUser ( ) ;
3334 if ( ! user ) throw new Error ( 'Utilisateur non connecté' ) ;
3435
3536 const { data, error } = await supabase
3637 . from ( 'shopify_integrations' )
3738 . select ( '*' )
3839 . eq ( 'campaign_id' , campaignId )
40+ . eq ( 'user_id' , user . uid )
3941 . eq ( 'active' , true )
4042 . order ( 'created_at' , { ascending : false } ) ;
4143
@@ -60,7 +62,7 @@ export const useShopifySupabase = (campaignId: string) => {
6062 variant : "destructive"
6163 } ) ;
6264 }
63- } , [ campaignId ] ) ;
65+ } , [ campaignId , user ] ) ;
6466
6567 // Initier l'installation Shopify
6668 const initiateShopifyInstall = useCallback ( async ( shopName : string ) => {
@@ -76,15 +78,14 @@ export const useShopifySupabase = (campaignId: string) => {
7678 setIsLoading ( true ) ;
7779
7880 try {
79- const { data : { user } } = await supabase . auth . getUser ( ) ;
8081 if ( ! user ) throw new Error ( 'Utilisateur non connecté' ) ;
8182
8283 // Appeler l'Edge Function pour générer l'URL d'autorisation
8384 const { data, error } = await supabase . functions . invoke ( 'shopify-auth' , {
8485 body : {
8586 shop : shopName . includes ( '.myshopify.com' ) ? shopName : `${ shopName } .myshopify.com` ,
8687 campaignId,
87- userId : user . id
88+ userId : user . uid
8889 }
8990 } ) ;
9091
0 commit comments