From 8e5ffcd6e47d41986ba02a9922c46897eb3e72f2 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Thu, 26 Sep 2024 17:31:06 +0200 Subject: [PATCH] protected roads --- src/App.jsx | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index a6212b3..5e654be 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -2,7 +2,7 @@ import { useEffect, useState, Suspense, lazy } from 'react'; import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom'; import { ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; -import { auth } from './service/firebase'; +import { auth , getUserSubscription } from './service/firebase'; import styles from './App.module.scss'; import Loading from './pages/Loading/loading'; import Pricing from './pages/Payement/Pricing/Pricing'; @@ -21,6 +21,7 @@ const App = () => { const [user, setUser] = useState(() => JSON.parse(localStorage.getItem('user')) || null); const [loading, setLoading] = useState(true); const [showLoading, setShowLoading] = useState(false); + const [subscription, setSubscription] = useState(0); useEffect(() => { const timeoutId = setTimeout(() => setShowLoading(true), 6000); @@ -28,10 +29,11 @@ const App = () => { const unsubscribe = auth.onAuthStateChanged((user) => { if (user) { localStorage.setItem('user', JSON.stringify(user)); + setUser(user); } else { localStorage.removeItem('user'); + setUser(null); } - setUser(user); setLoading(false); clearTimeout(timeoutId); }); @@ -42,6 +44,20 @@ const App = () => { }; }, []); + useEffect(() => { + if (user) { + const fetchSubscription = async () => { + try { + const userSubscription = await getUserSubscription(user.uid); + setSubscription(userSubscription || 0); + } catch (error) { + console.error('Error fetching subscription:', error); + } + }; + fetchSubscription(); + } + }, [user]); + if (loading && showLoading) { return ; } @@ -51,20 +67,34 @@ const App = () => {
}> + {/* Public Route */} : } /> + + {/* Protected Routes (Requires Authentication) */} : } /> : } /> - : } /> - : } /> - : } /> + + {/* Routes with Subscription Levels */} + 0 ? : ) : } /> + 1 ? : ) : } /> + 2 ? : ) : } /> + + {/* Server Details Route */} : } /> + + {/* Pricing and Payment */} : } /> : } /> : } /> + + {/* Default Route */} } /> + + {/* Catch-all route */} : } /> +