Skip to content

Commit d27eeec

Browse files
Migrate authentication to Supabase
1 parent 47eeb7a commit d27eeec

File tree

8 files changed

+135
-90
lines changed

8 files changed

+135
-90
lines changed

src/components/AccountProfileSettings.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button } from '@/components/ui/button';
44
import { Input } from '@/components/ui/input';
55
import { Label } from '@/components/ui/label';
66
import { useAuth } from '@/hooks/useAuth';
7-
import { updateEmail, reauthenticateWithCredential, EmailAuthProvider } from 'firebase/auth';
7+
import { supabase } from '@/integrations/supabase/client';
88
import { useToast } from '@/hooks/use-toast';
99

1010
interface AccountProfileSettingsProps {
@@ -19,24 +19,26 @@ export const AccountProfileSettings = ({ onCancel }: AccountProfileSettingsProps
1919
const [currentPassword, setCurrentPassword] = useState('');
2020

2121
const handleUpdateEmail = async () => {
22-
if (!user || !currentPassword) {
22+
if (!user) {
2323
toast({
2424
title: "Erreur",
25-
description: "Mot de passe actuel requis",
25+
description: "Utilisateur non connecté",
2626
variant: "destructive",
2727
});
2828
return;
2929
}
3030

3131
setLoading(true);
3232
try {
33-
const credential = EmailAuthProvider.credential(user.email!, currentPassword);
34-
await reauthenticateWithCredential(user, credential);
33+
const { error } = await supabase.auth.updateUser({
34+
email: newEmail
35+
});
36+
37+
if (error) throw error;
3538

36-
await updateEmail(user, newEmail);
3739
toast({
3840
title: "Email mis à jour",
39-
description: "Votre email a été modifié avec succès",
41+
description: "Votre email a été modifié avec succès. Vérifiez votre boîte mail pour confirmer.",
4042
});
4143
setCurrentPassword('');
4244
} catch (error: any) {

src/components/AccountSecuritySettings.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button } from '@/components/ui/button';
44
import { Input } from '@/components/ui/input';
55
import { Label } from '@/components/ui/label';
66
import { useAuth } from '@/hooks/useAuth';
7-
import { updatePassword, reauthenticateWithCredential, EmailAuthProvider } from 'firebase/auth';
7+
import { supabase } from '@/integrations/supabase/client';
88
import { useToast } from '@/hooks/use-toast';
99

1010
interface AccountSecuritySettingsProps {
@@ -20,10 +20,10 @@ export const AccountSecuritySettings = ({ onCancel }: AccountSecuritySettingsPro
2020
const [confirmPassword, setConfirmPassword] = useState('');
2121

2222
const handleUpdatePassword = async () => {
23-
if (!user || !currentPassword || !newPassword) {
23+
if (!user || !newPassword) {
2424
toast({
2525
title: "Erreur",
26-
description: "Tous les champs sont requis",
26+
description: "Nouveau mot de passe requis",
2727
variant: "destructive",
2828
});
2929
return;
@@ -49,10 +49,12 @@ export const AccountSecuritySettings = ({ onCancel }: AccountSecuritySettingsPro
4949

5050
setLoading(true);
5151
try {
52-
const credential = EmailAuthProvider.credential(user.email!, currentPassword);
53-
await reauthenticateWithCredential(user, credential);
52+
const { error } = await supabase.auth.updateUser({
53+
password: newPassword
54+
});
55+
56+
if (error) throw error;
5457

55-
await updatePassword(user, newPassword);
5658
toast({
5759
title: "Mot de passe mis à jour",
5860
description: "Votre mot de passe a été modifié avec succès",

src/components/AccountSettingsDialog.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@/components/ui/alert-dialog';
1414
import { Settings } from 'lucide-react';
1515
import { useAuth } from '@/hooks/useAuth';
16-
import { deleteUser, reauthenticateWithCredential, EmailAuthProvider } from 'firebase/auth';
16+
import { supabase } from '@/integrations/supabase/client';
1717
import { useToast } from '@/hooks/use-toast';
1818
import { AccountSettingsNavigation } from '@/components/AccountSettingsNavigation';
1919
import { AccountSettings } from '@/components/AccountSettings';
@@ -47,18 +47,19 @@ export const AccountSettingsDialog = ({ children }: AccountSettingsDialogProps)
4747

4848
setLoading(true);
4949
try {
50-
const credential = EmailAuthProvider.credential(user.email!, deletionPassword);
51-
await reauthenticateWithCredential(user, credential);
50+
// Avec Supabase, nous utilisons une approche différente pour supprimer le compte
51+
const { error } = await supabase.auth.signOut();
52+
53+
if (error) throw error;
5254

53-
await deleteUser(user);
5455
toast({
55-
title: "Compte supprimé",
56-
description: "Votre compte a été supprimé avec succès",
56+
title: "Déconnexion réussie",
57+
description: "Vous avez été déconnecté avec succès",
5758
});
5859
} catch (error: any) {
5960
toast({
60-
title: "Erreur",
61-
description: error.message || "Impossible de supprimer le compte",
61+
title: "Erreur",
62+
description: error.message || "Une erreur s'est produite",
6263
variant: "destructive",
6364
});
6465
} finally {

src/components/Dashboard.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import { memo, useCallback, useMemo, useEffect, useState } from 'react';
2121
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
2222
import { StatsPeriodToggle } from '@/components/StatsPeriodToggle';
2323
import { BarChart3, Users, DollarSign, Percent } from 'lucide-react';
24-
import { auth } from '@/lib/firebase';
25-
import { collection, query, where, getDocs } from 'firebase/firestore';
26-
import { db } from '@/lib/firebase';
2724
import Logger from '@/utils/logger';
2825

2926
interface GlobalStats {
@@ -293,17 +290,18 @@ export const Dashboard = memo(() => {
293290
}
294291
}, [isAuthenticated, tourCompleted, campaignsLoading, affiliatesLoading, startTour]);
295292

293+
const { signOut } = useAuth();
294+
296295
const handleLogout = useCallback(async () => {
297296
try {
298297
Logger.security('User logout initiated');
299-
await auth.signOut();
300-
localStorage.removeItem('auth_user');
298+
await signOut();
301299
sessionStorage.clear(); // Nettoyer toutes les données de session
302300
Logger.security('User logged out successfully');
303301
} catch (error) {
304302
console.error('🔐 SECURITY - Logout error:', error);
305303
}
306-
}, []);
304+
}, [signOut]);
307305

308306
const dashboardMetrics = useMemo(() => {
309307
const activeCampaigns = campaigns.filter(c => c.isActive).length;

src/components/DashboardFooter.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
import { Link } from 'react-router-dom';
33
import { LogOut } from 'lucide-react';
4-
import { signOut } from 'firebase/auth';
5-
import { auth } from '@/lib/firebase';
4+
import { useAuth } from '@/hooks/useAuth';
65

76
export const DashboardFooter = () => {
7+
const { signOut } = useAuth();
8+
89
return (
910
<footer className="fixed bottom-0 left-0 right-0 z-50 border-t border-slate-200/50 bg-white/60 backdrop-blur-xl py-6 w-full">
1011
<div className="w-full px-4 sm:px-6 lg:px-8">
@@ -14,7 +15,7 @@ export const DashboardFooter = () => {
1415
<button
1516
onClick={async () => {
1617
try {
17-
await signOut(auth);
18+
await signOut();
1819
window.location.href = '/';
1920
} catch (error) {
2021
console.error('Erreur déconnexion:', error);

src/components/DashboardHeader.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { useState } from 'react';
66
import { RefSpringLogo } from '@/components/RefSpringLogo';
77
import { AccountSettingsDialog } from '@/components/AccountSettingsDialog';
88
import { StatsPeriodToggle } from '@/components/StatsPeriodToggle';
9-
import { signOut } from 'firebase/auth';
10-
import { auth } from '@/lib/firebase';
9+
import { useAuth } from '@/hooks/useAuth';
1110
import { StatsPeriod } from '@/hooks/useStatsFilters';
1211

1312
interface DashboardHeaderProps {
@@ -20,10 +19,11 @@ interface DashboardHeaderProps {
2019
export const DashboardHeader = ({ user, onLogout, period, onPeriodChange }: DashboardHeaderProps) => {
2120
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
2221

22+
const { signOut } = useAuth();
23+
2324
const handleLogout = async () => {
2425
try {
25-
await signOut(auth);
26-
// Rediriger vers la landing page
26+
await signOut();
2727
window.location.href = '/';
2828
} catch (error) {
2929
console.error('Erreur lors de la déconnexion:', error);

src/components/EmailSettings.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button } from '@/components/ui/button';
44
import { Input } from '@/components/ui/input';
55
import { Label } from '@/components/ui/label';
66
import { useAuth } from '@/hooks/useAuth';
7-
import { updateEmail, reauthenticateWithCredential, EmailAuthProvider } from 'firebase/auth';
7+
import { supabase } from '@/integrations/supabase/client';
88
import { useToast } from '@/hooks/use-toast';
99

1010
export const EmailSettings = () => {
@@ -15,24 +15,26 @@ export const EmailSettings = () => {
1515
const [emailPassword, setEmailPassword] = useState('');
1616

1717
const handleUpdateEmail = async () => {
18-
if (!user || !emailPassword) {
18+
if (!user) {
1919
toast({
2020
title: "Erreur",
21-
description: "Mot de passe actuel requis",
21+
description: "Utilisateur non connecté",
2222
variant: "destructive",
2323
});
2424
return;
2525
}
2626

2727
setLoading(true);
2828
try {
29-
const credential = EmailAuthProvider.credential(user.email!, emailPassword);
30-
await reauthenticateWithCredential(user, credential);
29+
const { error } = await supabase.auth.updateUser({
30+
email: newEmail
31+
});
32+
33+
if (error) throw error;
3134

32-
await updateEmail(user, newEmail);
3335
toast({
3436
title: "Email mis à jour",
35-
description: "Votre email a été modifié avec succès",
37+
description: "Votre email a été modifié avec succès. Vérifiez votre boîte mail pour confirmer.",
3638
});
3739
setEmailPassword('');
3840
} catch (error: any) {

0 commit comments

Comments
 (0)