From 366dc07bb6b18cec7d9a36c2951e6d04c20ddd53 Mon Sep 17 00:00:00 2001 From: Harshitha Shetty <141444342+HarshithaShetty27@users.noreply.github.com> Date: Fri, 14 Mar 2025 12:43:12 +0530 Subject: [PATCH] admin visibilty- temp fix --- client/src/Pages/Login.jsx | 39 ++++++-- client/src/Pages/Navbar.jsx | 183 +++++++++++++++++++++++++++++------ server/models/User.js | 4 + server/routes/emailRoutes.js | 6 +- 4 files changed, 193 insertions(+), 39 deletions(-) diff --git a/client/src/Pages/Login.jsx b/client/src/Pages/Login.jsx index 5a57da2..abf1528 100644 --- a/client/src/Pages/Login.jsx +++ b/client/src/Pages/Login.jsx @@ -15,6 +15,10 @@ function AuthPage() { }); const [signin, setSignin] = useState(false); + const adminEmails = [ + "harshitha.ss@somaiya.edu", + ]; + const notifyError = (message) => { toast.error(message); }; @@ -70,19 +74,22 @@ function AuthPage() { )}?d=identicon`; user.profilePicture = gravatarUrl; + if (adminEmails.includes(user.email)) { + await axios.post(`http://localhost:8080/api/user/make-admin/${user._id}`); + const updatedUser = await axios.get(`http://localhost:8080/api/user/${user._id}`); + user.isAdmin = updatedUser.data.isAdmin; + localStorage.setItem("isAdmin", user.isAdmin); + } else { + localStorage.setItem("isAdmin", false); + } + + // ✅ Save user to localStorage + localStorage.setItem("user", JSON.stringify(user)); window.location.href = "/Welcome"; } } catch (error) { console.error("Authentication error:", error); - if ( - error.response && - error.response.status === 400 && - error.response.data.message === "User already exists" - ) { - notifyError("User already exists"); - } else { - notifyError(error.response?.data.message || "An error occurred"); - } + notifyError(error.response?.data.message || "An error occurred"); } } @@ -91,6 +98,20 @@ function AuthPage() { window.location.href = "http://localhost:8080/auth/google"; }; + useEffect(() => { + const queryParams = new URLSearchParams(window.location.search); + const email = queryParams.get("email"); + + if (email) { + if (adminEmails.includes(email)) { + localStorage.setItem("isAdmin", true); + } else { + localStorage.setItem("isAdmin", false); + } + } + }, []); + + return ( <> diff --git a/client/src/Pages/Navbar.jsx b/client/src/Pages/Navbar.jsx index 9ddc364..bef01e5 100644 --- a/client/src/Pages/Navbar.jsx +++ b/client/src/Pages/Navbar.jsx @@ -1,7 +1,6 @@ import React, { useEffect, useState } from "react"; import { FaUserCircle } from "react-icons/fa"; import { NavLink, useNavigate } from "react-router-dom"; // Import NavLink for navigation -import "./Navbar.css"; // Navbar-specific styles import axios from "axios"; import { toast, ToastContainer } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; @@ -9,21 +8,61 @@ import 'react-toastify/dist/ReactToastify.css'; const Navbar = () => { const navigate = useNavigate(); const [user, setUser] = useState(null); + const [isAdmin, setIsAdmin] = useState(false); + + // useEffect(() => { + // const loggedInUser = localStorage.getItem("user"); + // const adminStatus = localStorage.getItem("isAdmin"); + + // if (loggedInUser) { + // // ✅ Set user from localStorage + // setUser(JSON.parse(loggedInUser)); + // if (adminStatus === "true") setIsAdmin(true); + // } + + // // ✅ Even after refresh, reconfirm the user from backend + // axios.get("http://localhost:8080/api/user/profile", { withCredentials: true }) + // .then((response) => { + // const userData = response.data.user; + // setUser(userData); + // console.log(userData); + + // // ✅ Check if the user is admin (FROM BACKEND) + // if (userData.isAdmin == true) { + // setIsAdmin(true); + // localStorage.setItem("isAdmin", true); + // } else { + // setIsAdmin(false); + // localStorage.setItem("isAdmin", false); + // } + // console.log(isAdmin); + // }) + // .catch((error) => { + // console.error("Error fetching user data:", error); + // toast.error("Failed to fetch user data."); + // }); + // }, []); useEffect(() => { - const loggedInUser = localStorage.getItem("user"); - if (loggedInUser) { - setUser(JSON.parse(loggedInUser)); - } else { - axios.get("http://localhost:8080/api/user/profile", { withCredentials: true }) - .then((response) => { - setUser(response.data.user); - }) - .catch((error) => { - console.error("Error fetching user data:", error); - }); - } - }, []); + axios.get("http://localhost:8080/api/user/profile", { withCredentials: true }) + .then((response) => { + const userData = response.data.user; + setUser(userData); + + if (userData.isAdmin) { + setIsAdmin(true); + localStorage.setItem("isAdmin", "true"); + } else { + setIsAdmin(false); + localStorage.setItem("isAdmin", "false"); + } + + }) + .catch((error) => { + console.error("Error fetching user data:", error); + }); + }, [user]); // ✅ Add `user` dependency to re-run when `user` updates + // Handle logout functionality const handleLogout = async () => { @@ -41,31 +80,113 @@ const Navbar = () => { }; return ( -
-
+
+
- - Logo + + Logo -
+
{/* Consolidated buttons in the center */} - + e.target.style.backgroundColor = "#660000"} + onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}> Faculty Consolidated - + e.target.style.backgroundColor = "#660000"} + onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}> Course Consolidated - + e.target.style.backgroundColor = "#660000"} + onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}> Department Consolidated + e.target.style.backgroundColor = "#660000"} + onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}> + Panel + + {isAdmin && ( + e.target.style.backgroundColor = "#660000"} + onMouseLeave={(e) => e.target.style.backgroundColor = "#B22222"}> + Faculty + + )}
-
@@ -76,15 +197,23 @@ const Navbar = () => { Profile ) : ( - + )}
-
+
); }; diff --git a/server/models/User.js b/server/models/User.js index ad6b056..b4ece36 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -16,6 +16,10 @@ const UserSchema = new mongoose.Schema( type: Date, default: null, }, + isAdmin:{ + type:Boolean, + default: false, + } }, { timestamps: true, diff --git a/server/routes/emailRoutes.js b/server/routes/emailRoutes.js index 95abb49..dfa193c 100644 --- a/server/routes/emailRoutes.js +++ b/server/routes/emailRoutes.js @@ -23,9 +23,9 @@ const transporter = nodemailer.createTransport({ pass: "umlc hbkr dpga iywd", }, tls: { rejectUnauthorized: false }, - connectionTimeout: 30000, - greetingTimeout: 30000, - socketTimeout: 30000, + connectionTimeout: 60000, + greetingTimeout: 60000, + socketTimeout: 60000, }); // Existing Excel route unchanged, except transporter removal