Skip to content

Commit

Permalink
🐛 fix: I fixed a bug in authentication and in the server - v.0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mpcgt committed Dec 2, 2024
1 parent f2a1512 commit 1e14ecf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/api/supabase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
64 changes: 42 additions & 22 deletions src/hooks/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -15,38 +15,58 @@ interface AuthProviderProps {

export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState<boolean>(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 <div>Loading...</div>;
}

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 <Navigate to="/profile" replace />;
}

return (
<AuthContext.Provider value={{ user }}>
Expand Down

0 comments on commit 1e14ecf

Please sign in to comment.