-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #286 from Ansh101112/adb
Admin Dashboard added
- Loading branch information
Showing
6 changed files
with
58 additions
and
18 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
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
// AuthRoute.js | ||
import React from "react"; | ||
import { Navigate, Outlet } from "react-router-dom"; | ||
import { Route, Navigate } from "react-router-dom"; | ||
|
||
const AuthRoute = () => { | ||
const AuthRoute = ({ element, ...rest }) => { | ||
// Check if user is authenticated and has admin role | ||
const isLoggedIn = localStorage.getItem("isLoggedIn") === "true"; | ||
const userRole = localStorage.getItem("role"); // Assuming user role is stored in localStorage | ||
const currentUser = JSON.parse(localStorage.getItem("currentUser")); | ||
const isAdmin = currentUser && currentUser.role === 1; | ||
|
||
if (!isLoggedIn) { | ||
// Redirect to login if not authenticated or not an admin | ||
if (!isLoggedIn || !isAdmin) { | ||
return <Navigate to="/user/login" />; | ||
} | ||
|
||
if (userRole !== "admin") { | ||
return <Navigate to="/" />; | ||
} | ||
|
||
return <Outlet />; | ||
return <Route {...rest} element={element} />; | ||
}; | ||
|
||
export default AuthRoute; |
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 |
---|---|---|
|
@@ -42,16 +42,38 @@ const Login = () => { | |
}); | ||
|
||
if (response && response.data.success) { | ||
toast.success("Login successful!"); | ||
localStorage.setItem("isLoggedIn", true); | ||
localStorage.setItem("isLoggedIn", "true"); | ||
localStorage.setItem("token", response.data.token); | ||
|
||
// Decode the token to get the user role | ||
|
||
|
||
const tokenPayload = JSON.parse( | ||
atob(response.data.token.split(".")[1]) | ||
); | ||
const userRole = tokenPayload.role; | ||
|
||
|
||
// Store user role in localStorage | ||
localStorage.setItem( | ||
"currentUser", | ||
JSON.stringify({ email, role: userRole }) | ||
); | ||
|
||
if (email === "[email protected]" && password === "123456") { | ||
toast.success("Admin Logged in successfully!"); | ||
navigate("/admin/dashboard"); | ||
} else { | ||
toast.success("Login successful!"); | ||
setTimeout(() => { | ||
if (userRole === 1) { | ||
navigate("/admin/dashboard"); | ||
} else { | ||
navigate("/"); | ||
} | ||
}, 2000); | ||
} | ||
|
||
localStorage.setItem( | ||
"currentUser", | ||
JSON.stringify({ name: "User Name", role: userRole }) | ||
|
@@ -65,6 +87,7 @@ const Login = () => { | |
navigate("/"); | ||
} | ||
}, 2000); | ||
|
||
} | ||
} catch (error) { | ||
if (error.response) { | ||
|
@@ -88,7 +111,7 @@ const Login = () => { | |
return ( | ||
<> | ||
<Navbar /> | ||
<ToastContainer></ToastContainer> | ||
<ToastContainer /> | ||
<div className="flex items-center justify-center min-h-screen bg-gray-100"> | ||
<div className="w-full max-w-md p-8 space-y-6 bg-white rounded-lg shadow-2xl"> | ||
<div className="flex justify-center"> | ||
|