Adding login auth
This commit is contained in:
+15
-27
@@ -1,37 +1,25 @@
|
||||
import React from "react";
|
||||
import { BrowserRouter as Router, Routes, Route, useNavigate } from "react-router-dom";
|
||||
import { Routes, Route, Navigate } from "react-router-dom";
|
||||
import Login from "./pages/Login";
|
||||
import Register from "./pages/Register";
|
||||
import { useAuth } from "./AuthContext";
|
||||
import Dashboard from "./pages/Dashboard";
|
||||
import Layout from "./components/Layout";
|
||||
|
||||
const App = () => {
|
||||
const { isAuthenticated, logout } = useAuth();
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
return (
|
||||
<div style={{ height: "100vh", display: "flex", flexDirection: "column" }}>
|
||||
{/* Logout button */}
|
||||
{isAuthenticated && (
|
||||
<div style={{ display: "flex", justifyContent: "flex-end", padding: "1rem" }}>
|
||||
<button onClick={logout}>Logout</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Centered content */}
|
||||
<div
|
||||
style={{
|
||||
flex: 1,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
<Routes>
|
||||
<Route path="/" element={<Login />} />
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
</Routes>
|
||||
</div>
|
||||
</div>
|
||||
<Layout>
|
||||
<Routes>
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
<Route
|
||||
path="/dashboard"
|
||||
element={token ? <Dashboard /> : <Navigate to="/login" />}
|
||||
/>
|
||||
<Route path="*" element={<Navigate to="/login" />} />
|
||||
</Routes>
|
||||
</Layout>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user