From 1e14ecf7c07c3b12fcdb15b2c761c9a97d79158c Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 2 Dec 2024 12:34:20 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20I=20fixed=20a=20bug=20in?= =?UTF-8?q?=20authentication=20and=20in=20the=20server=20-=20v.0.3.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/supabase.tsx | 2 +- src/hooks/AuthContext.tsx | 64 +++++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/api/supabase.tsx b/src/api/supabase.tsx index e8b78b2..b8fac52 100644 --- a/src/api/supabase.tsx +++ b/src/api/supabase.tsx @@ -6,7 +6,7 @@ function isString(value: string | undefined): value is string { const supabaseUrl = import.meta.env.VITE_SUPABASE_URL; const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY; -const supabaseServiceRole = import.meta.env.VITE_SUPABASE_SERVICE_ROLE; +export const supabaseServiceRole = import.meta.env.VITE_SUPABASE_SERVICE_ROLE; if (!isString(supabaseUrl) || !isString(supabaseAnonKey) || !isString(supabaseServiceRole)) { diff --git a/src/hooks/AuthContext.tsx b/src/hooks/AuthContext.tsx index 2832148..4a5f680 100644 --- a/src/hooks/AuthContext.tsx +++ b/src/hooks/AuthContext.tsx @@ -1,5 +1,5 @@ import React, { createContext, useState, useEffect, ReactNode } from 'react'; -import { useNavigate } from 'react-router-dom'; +import { useNavigate, useLocation, Navigate } from 'react-router-dom'; import { supabase } from '../api/supabase'; import type { User } from '@supabase/supabase-js'; @@ -15,38 +15,58 @@ interface AuthProviderProps { export const AuthProvider: React.FC = ({ children }) => { const [user, setUser] = useState(null); + const [loading, setLoading] = useState(true); const navigate = useNavigate(); + const location = useLocation(); + + // Définissez les routes publiques + const publicRoutes = ['/about', '/contact', '/home']; // Ajoutez ici toutes les routes accessibles sans connexion useEffect(() => { async function checkUser() { - const { data: sessionData, error: sessionError } = await supabase.auth.getSession(); - if (sessionError || !sessionData.session) { - setUser(null); - navigate('/profile'); - return; - } + try { + const { data: { user }, error } = await supabase.auth.getUser(); + + if (error) { + console.error("Error fetching current user:", error.message); + setUser(null); + setLoading(false); + return; + } - const { data, error } = await supabase.auth.getUser(); - if (error) { - console.error("Error fetching user:", error.message); - } else { - setUser(data?.user ?? null); + setUser(user); + setLoading(false); + } catch (error) { + console.error("Error fetching current user:", error); + setUser(null); + setLoading(false); } } checkUser(); - const { data: authListener } = supabase.auth.onAuthStateChange((_event, session) => { - setUser(session ? session.user : null); - if (!session) { - navigate('/profile'); - } - }); + if (supabase.auth) { + const { data: authListener } = supabase.auth.onAuthStateChange((_event, session) => { + if (!session) { + setUser(null); + } else { + checkUser(); + } + }); + return () => { + authListener?.subscription.unsubscribe(); + }; + } + }, [navigate, location.pathname]); + + if (loading) { + return
Loading...
; + } - return () => { - authListener?.subscription.unsubscribe(); - }; - }, [navigate]); + // Autorisez l'accès aux routes publiques même si l'utilisateur n'est pas connecté + if (!user && !publicRoutes.includes(location.pathname) && ['/discovery', '/profile'].includes(location.pathname)) { + return ; + } return (