-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.jsx
132 lines (121 loc) · 4.45 KB
/
App.jsx
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
122
123
124
125
126
127
128
129
130
131
132
import React, { useState, useEffect } from "react";
import { Route, Routes, Navigate, useNavigate } from "react-router-dom";
import { getAuth, onAuthStateChanged } from "firebase/auth";
import Header from "./components/Header";
import Flights from "./routes/Flights";
import Setting from "./routes/Setting";
import Notification from "./routes/Notification";
import Profile from "./routes/Profile";
import SignIn from "./auth/SignIn";
import AddFlight from "./routes/AddFlight";
const App = () => {
const [lastUpdated, setLastUpdated] = useState(new Date());
const [isLoading, setIsLoading] = useState(true);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [user, setUser] = useState({ name: "", email: "" });
const navigate = useNavigate();
const auth = getAuth();
// Check authentication status on mount
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (firebaseUser) => {
if (firebaseUser) {
// If user is authenticated, retrieve stored user data
const storedUserData = localStorage.getItem("userData");
if (storedUserData) {
const userData = JSON.parse(storedUserData);
// Update user state with stored data
setUser({
name: userData.name,
email: userData.email,
});
setIsAuthenticated(true);
// If on sign-in page, redirect to flights
if (window.location.pathname === "/") {
navigate("/flights");
}
}
}
setIsLoading(false);
});
// Cleanup subscription
return () => unsubscribe();
}, [auth, navigate]);
// Handles user login
// Once log-in successful, redirect to the flight (main) page
const handleLogin = (userData) => {
setUser({
name: userData.name, // Set the name from Google sign-in
email: userData.email, // Set the email from Google sign-in
});
setIsAuthenticated(true);
navigate("/flights"); // Redirect to Flights after successful sign-in
};
// Handles user logout
// Clears authentication state and navigates to sign-in page
const handleLogout = () => {
auth.signOut();
setUser({ name: "", email: "" });
localStorage.removeItem("authToken");
localStorage.removeItem("userData");
setIsAuthenticated(false);
navigate("/"); // Redirect to SignIn page after logout
};
// Handles data refresh
// Updates the lastUpdated timestamp to trigger data refresh
const handleRefresh = () => {
setLastUpdated(new Date()); // Update the last updated timestamp
};
// Show loading state while checking authentication
if (isLoading) {
return (
<div className="bg-[#DEE1E6] flex flex-row justify-center w-full rounded-xl">
<div className="bg-white w-[380px] h-[403px] flex items-center justify-center">
<p>Loading...</p>
</div>
</div>
);
}
return (
<div className="bg-[#DEE1E6] flex flex-row justify-center w-full rounded-xl">
<div className="bg-white overflow-hidden w-[380px] h-[403px] relative rounded-xl flex flex-col">
{/* Header Component */}
<Header isAuthenticated={isAuthenticated} onRefresh={handleRefresh} />
{/* Main Content */}
<div className="p-4 space-y-4 overflow-y-auto h-full scrollbar-hidden">
<Routes>
{/* Default Route - Sign In Page */}
<Route path="/" element={<SignIn onSignIn={handleLogin} />} />
{/* Protected Routes */}
{isAuthenticated ? (
<>
<Route
path="/flights"
element={<Flights lastUpdated={lastUpdated} />}
/>
<Route path="/setting" element={<Setting />} />
<Route path="/notification" element={<Notification />} />
<Route
path="/profile"
element={
<Profile
userName={user.name}
userEmail={user.email}
onLogout={handleLogout}
/>
}
/>
<Route path="/addflight" element={<AddFlight />} />
</>
) : (
// Redirect any unauthorized access to the Sign In page
<Route path="*" element={<Navigate to="/" />} />
)}
</Routes>
{/* Footer Component */}
{/* <Footer /> */}
</div>
</div>
</div>
);
};
export default App;