-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.tsx
More file actions
66 lines (59 loc) · 2.12 KB
/
App.tsx
File metadata and controls
66 lines (59 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useEffect, useState } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { Session } from '@supabase/supabase-js';
import { supabase } from './backend/supabaseClient';
import Dashboard from './pages/Dashboard';
import Login from './pages/Login';
import Register from './pages/Register';
const App: React.FC = () => {
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
// Check active session
supabase.auth.getSession().then(({ data: { session } }) => {
setSession(session);
setLoading(false);
});
// Listen for auth changes
const {
data: { subscription },
} = supabase.auth.onAuthStateChange((_event, session) => {
setSession(session);
setLoading(false);
});
return () => subscription.unsubscribe();
}, []);
if (loading) {
return (
<div className="flex items-center justify-center min-h-screen bg-zinc-900 text-teal-400">
<svg className="animate-spin h-8 w-8" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</div>
);
}
return (
<BrowserRouter>
<Routes>
<Route
path="/"
element={session ? <Navigate to="/dashboard" replace /> : <Navigate to="/login" replace />}
/>
<Route
path="/login"
element={!session ? <Login /> : <Navigate to="/dashboard" replace />}
/>
<Route
path="/register"
element={!session ? <Register /> : <Navigate to="/dashboard" replace />}
/>
<Route
path="/dashboard"
element={session ? <Dashboard session={session} /> : <Navigate to="/login" replace />}
/>
</Routes>
</BrowserRouter>
);
};
export default App;