-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Eht_new_working_branch
- Loading branch information
Showing
10 changed files
with
469 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
import React, { Component } from 'react'; | ||
import { Route, Routes } from 'react-router-dom'; | ||
//import AppRoutes from './AppRoutes'; | ||
import { Layout } from './components/Layout'; | ||
import Home from './components/Home'; | ||
import Login from './components/Login'; | ||
import AboutUs from './components/AboutUs'; | ||
import SignUp from './components/SignUp'; | ||
import AppInterface from './components/AppInterface'; | ||
|
||
// App.js | ||
import React, { Component } from "react"; | ||
import { Route, Routes, BrowserRouter } from "react-router-dom"; | ||
import { AuthProvider } from "./AuthContext"; | ||
import { Layout } from "./components/Layout"; | ||
import Home from "./components/Home"; | ||
import Login from "./components/Login"; | ||
import AboutUs from "./components/AboutUs"; | ||
import SignUp from "./components/SignUp"; | ||
import AppInterface from "./components/AppInterface"; | ||
import ProtectedRoute from "./ProtectedRoute"; | ||
export default class App extends Component { | ||
static displayName = App.name; | ||
|
||
render() { | ||
return ( | ||
<div className=""> | ||
<div className=""> | ||
<Layout/> | ||
<Routes> | ||
<Route path="/" element={<Home/>} /> | ||
<Route path="/AboutUs" element={<AboutUs/>}/> | ||
<Route path="/Login" element={<Login/>}/> | ||
<Route path="/Signup" element={<SignUp/>}/> | ||
<Route path="/App" element={<AppInterface/>}/> | ||
</Routes> | ||
|
||
</div> | ||
</div> | ||
<AuthProvider> | ||
<BrowserRouter> | ||
<Layout /> | ||
<Routes> | ||
<Route path="/" element={<Home />} /> | ||
<Route path="/AboutUs" element={<AboutUs />} /> | ||
<Route path="/Login" element={<Login />} /> | ||
<Route path="/Signup" element={<SignUp />} /> | ||
<Route path="/App" element={<ProtectedRoute />}> | ||
<Route index element={<AppInterface />} /> | ||
</Route> | ||
</Routes> | ||
</BrowserRouter> | ||
</AuthProvider> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { createContext, useState, useEffect } from "react"; | ||
|
||
// Create authentication context | ||
export const AuthContext = createContext(null); | ||
|
||
// Provider component for managing auth state | ||
export const AuthProvider = ({ children }) => { | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
|
||
// Check session storage for login state on component mount | ||
useEffect(() => { | ||
const userLoggedIn = sessionStorage.getItem("isLoggedIn") === "true"; | ||
setIsLoggedIn(userLoggedIn); | ||
}, []); | ||
|
||
// Set login state and store in session storage | ||
const login = () => { | ||
setIsLoggedIn(true); | ||
sessionStorage.setItem("isLoggedIn", "true"); | ||
}; | ||
|
||
// Implement the logout function | ||
const logout = () => { | ||
// Clear the session storage or any other persistent storage used | ||
sessionStorage.removeItem("isLoggedIn"); | ||
// Update the state to reflect the user is logged out | ||
setIsLoggedIn(false); | ||
}; | ||
|
||
// Provide auth state and handlers to child components | ||
return ( | ||
<AuthContext.Provider value={{ isLoggedIn, login, logout }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { useContext } from "react"; | ||
import { Navigate, Outlet } from "react-router-dom"; | ||
import { AuthContext } from "./AuthContext"; | ||
|
||
const ProtectedRoute = () => { | ||
const { isLoggedIn } = useContext(AuthContext); | ||
|
||
// If not logged in, redirect to the login page | ||
if (!isLoggedIn) { | ||
return <Navigate to="/Login" replace />; | ||
} | ||
|
||
// If logged in, render the child components | ||
return <Outlet />; | ||
}; | ||
|
||
export default ProtectedRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.