-
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.
"Added authentication and authorization features to Admin dashboard, …
…including login and logout functionality, protected routes, and admin role"
- Loading branch information
1 parent
4d8c231
commit b4afaad
Showing
14 changed files
with
440 additions
and
30 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
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,16 @@ | ||
import React from 'react'; | ||
import { Outlet } from 'react-router-dom'; | ||
import Sidebar from './Sidebar/Sidebar'; | ||
|
||
const Layout = () => { | ||
return ( | ||
<div className="app-content"> | ||
<Sidebar /> | ||
<div className="app-side-section"> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Layout; |
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,10 @@ | ||
import { useContext } from 'react'; | ||
import { Navigate } from 'react-router-dom'; | ||
import { StoreContext } from '../context/StoreContext'; | ||
|
||
const ProtectedRoute = ({ children }) => { | ||
const { isAuthenticated } = useContext(StoreContext); | ||
return isAuthenticated ? children : <Navigate to="/login" />; | ||
}; | ||
|
||
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
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,32 @@ | ||
import { createContext, useEffect, useState } from "react"; | ||
|
||
export const StoreContext = createContext(null); | ||
|
||
const StoreContextProvider = (props) => { | ||
const [token, setToken] = useState(""); | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
|
||
useEffect(() => { | ||
const savedToken = localStorage.getItem("token"); | ||
if (savedToken) { | ||
setToken(savedToken); | ||
setIsAuthenticated(true); | ||
} | ||
else{ | ||
setIsAuthenticated(false); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<StoreContext.Provider value={{ | ||
token, | ||
setToken, | ||
isAuthenticated, | ||
setIsAuthenticated | ||
}}> | ||
{props.children} | ||
</StoreContext.Provider> | ||
); | ||
}; | ||
|
||
export default StoreContextProvider; |
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,11 +1,16 @@ | ||
import { BrowserRouter } from 'react-router-dom' | ||
import { createRoot } from 'react-dom/client' | ||
import App from './App.jsx' | ||
import './index.css' | ||
|
||
import React from 'react'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { createRoot } from 'react-dom/client'; | ||
import App from './App.jsx'; | ||
import StoreContextProvider from './context/StoreContext.jsx'; | ||
import './index.css'; | ||
|
||
createRoot(document.getElementById('root')).render( | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter>, | ||
) | ||
<React.StrictMode> | ||
<StoreContextProvider> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</StoreContextProvider> | ||
</React.StrictMode>, | ||
); |
Oops, something went wrong.