File size: 3,391 Bytes
2070fe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { useEffect } from 'react';
import { BrowserRouter as Router, Routes, Route, useNavigate, useLocation } from 'react-router-dom';
import { AuthProvider, useAuth } from './context/AuthContext';
import Hero from './components/Hero';
import Dashboard from './components/Dashboard';
import VideoRoom from './components/VideoRoom';
import ProtectedRoute from './components/ProtectedRoute';
import Snippets from './components/Snippets';
import Footer from './components/Footer';
import Login from './components/Login';
import Navbar from './components/Navbar';

// OAuth Handler Component
function OAuthHandler() {
  const navigate = useNavigate();
  const location = useLocation();
  const { checkAuth } = useAuth();

  useEffect(() => {
    const handleOAuthRedirect = async () => {
      const params = new URLSearchParams(location.search);
      
      // Check for successful OAuth
      if (params.get('auth') === 'success') {
        console.log('OAuth success detected, checking authentication...');
        
        try {
          // Verify authentication with backend
          const response = await fetch('/api/auth/me', {
            credentials: 'include'
          });
          
          const data = await response.json();
          
          if (response.ok && data.user) {
            console.log('User authenticated:', data.user);
            // Update auth context if you have a method for it
            if (checkAuth) {
              await checkAuth();
            }
            // Navigate to dashboard
            navigate('/dashboard', { replace: true });
          } else {
            console.error('Authentication verification failed');
            navigate('/login?error=verification_failed', { replace: true });
          }
        } catch (error) {
          console.error('Error verifying authentication:', error);
          navigate('/login?error=verification_error', { replace: true });
        }
        
        // Clean up URL parameters
        window.history.replaceState({}, '', location.pathname);
      }
      
      // Check for OAuth errors
      if (params.get('error')) {
        const errorMessage = params.get('error');
        console.error('OAuth error:', errorMessage);
        navigate(`/login?error=${errorMessage}`, { replace: true });
        // Clean up URL parameters
        window.history.replaceState({}, '', location.pathname);
      }
    };

    handleOAuthRedirect();
  }, [location, navigate, checkAuth]);

  return null;
}

function AppRoutes() {
  return (
    <>
      <OAuthHandler />
      <Routes>
        <Route path="/login" element={<Login />} />
        <Route
          path="/"
          element={
            <>
              <Hero />
              <Snippets />
              <Footer />
            </>
          }
        />
        <Route
          path="/dashboard"
          element={
            <ProtectedRoute>
              <Dashboard />
            </ProtectedRoute>
          }
        />
        <Route
          path="/room/:roomId"
          element={
            <ProtectedRoute>
              <VideoRoom />
            </ProtectedRoute>
          }
        />
      </Routes>
    </>
  );
}

function App() {
  return (
    <AuthProvider>
      <Router>
        <div className="App">
          <AppRoutes />
        </div>
      </Router>
    </AuthProvider>
  );
}

export default App;