Skip to content

Commit

Permalink
Merge pull request #286 from Ansh101112/adb
Browse files Browse the repository at this point in the history
Admin Dashboard added
  • Loading branch information
usha-madithati authored Jun 16, 2024
2 parents 206a57c + e3a3511 commit 51e9b5e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
4 changes: 4 additions & 0 deletions backend/Schemas/User.Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const UserSchema = new mongoose.Schema({
type: String,
required: true,
},
role: {
type: Number,
default: 0,
},
notificationPeriod: {
type: String,
default: "3 days",
Expand Down
4 changes: 4 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ app.use(cors());
// Connect to the database
dbConnect();



// Middleware to authenticate user
const authenticateUser = (req, res, next) => {
const token = req.headers.authorization;
Expand Down Expand Up @@ -280,6 +282,7 @@ app.post("/login", async (req, res) => {
return res.status(400).send({ message: "Invalid email or password" });
}


// Admin bypass check

const adminEmail = process.env.ADMINMAIL;
Expand All @@ -292,6 +295,7 @@ app.post("/login", async (req, res) => {
await user.save();
}


const token = jwt.sign(
{ userId: user._id, role: user.role },
process.env.JWT_SECRET,
Expand Down
4 changes: 1 addition & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const App = () => {
<Route path="/user/signup" element={<SignUp />} />
<Route path="/user/login" element={<Login />} />
<Route element={<PrivateRoute />}>
<Route path="/admin/dashboard" element={<AdminD></AdminD>} />
<Route path="/scanqr" element={<QRCodeVerification />} />
<Route path="/user/add-products" element={<PForm />} />
<Route path="/user/settings" element={<Settings></Settings>} />
Expand All @@ -37,9 +38,6 @@ const App = () => {
/>
</Route>
<Route path="/user/dashboard" element={<UserD></UserD>} />
<Route element={<AuthRoute />}>
<Route path="/admin/dashboard" element={<AdminD></AdminD>} />
</Route>
<Route path="/contact" element={<CustomerVoices />} />
<Route path="/user/review" element={<Review />} />
<Route path="*" element={<NotFoundPage></NotFoundPage>} />
Expand Down
18 changes: 9 additions & 9 deletions src/components/AuthRoute.js
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;
17 changes: 14 additions & 3 deletions src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Link } from "react-router-dom";

const Navbar = () => {
const isLoggedIn = localStorage.getItem("isLoggedIn") === "true";
const currentUser = JSON.parse(localStorage.getItem("currentUser"));
const userRole = currentUser?.role;

return (
<nav className="flex items-center justify-between px-6 py-4">
Expand All @@ -25,13 +27,21 @@ const Navbar = () => {
>
ABOUT
</Link>
{isLoggedIn ? (
{isLoggedIn && userRole === 1 && (
<Link
className="text-lg font-semibold hover:text-green-600"
to="/admin/dashboard"
>
ADMIN
</Link>
)}
{isLoggedIn && (
<>
<Link
className="text-lg font-semibold hover:text-green-600"
to="/user/dashboard"
>
USER
USER
</Link>
<Link
className="text-lg font-semibold hover:text-green-600"
Expand All @@ -40,7 +50,8 @@ const Navbar = () => {
GET NOTIFIED
</Link>
</>
) : (
)}
{!isLoggedIn && (
<Link
className="text-lg font-semibold hover:text-green-600"
to="/user/login"
Expand Down
29 changes: 26 additions & 3 deletions src/pages/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -65,6 +87,7 @@ const Login = () => {
navigate("/");
}
}, 2000);

}
} catch (error) {
if (error.response) {
Expand All @@ -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">
Expand Down

0 comments on commit 51e9b5e

Please sign in to comment.