admin visibilty- temp fix

This commit is contained in:
Harshitha Shetty
2025-03-14 12:43:12 +05:30
parent 19797290f3
commit 366dc07bb6
4 changed files with 193 additions and 39 deletions

View File

@@ -15,6 +15,10 @@ function AuthPage() {
}); });
const [signin, setSignin] = useState(false); const [signin, setSignin] = useState(false);
const adminEmails = [
"harshitha.ss@somaiya.edu",
];
const notifyError = (message) => { const notifyError = (message) => {
toast.error(message); toast.error(message);
}; };
@@ -70,27 +74,44 @@ function AuthPage() {
)}?d=identicon`; )}?d=identicon`;
user.profilePicture = gravatarUrl; 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"; window.location.href = "/Welcome";
} }
} catch (error) { } catch (error) {
console.error("Authentication error:", 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");
} }
} }
}
const handleGoogleLogin = (event) => { const handleGoogleLogin = (event) => {
event.preventDefault(); event.preventDefault();
window.location.href = "http://localhost:8080/auth/google"; 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 ( return (
<> <>
<ToastContainer /> <ToastContainer />

View File

@@ -1,7 +1,6 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import { FaUserCircle } from "react-icons/fa"; import { FaUserCircle } from "react-icons/fa";
import { NavLink, useNavigate } from "react-router-dom"; // Import NavLink for navigation import { NavLink, useNavigate } from "react-router-dom"; // Import NavLink for navigation
import "./Navbar.css"; // Navbar-specific styles
import axios from "axios"; import axios from "axios";
import { toast, ToastContainer } from 'react-toastify'; import { toast, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css'; import 'react-toastify/dist/ReactToastify.css';
@@ -9,21 +8,61 @@ import 'react-toastify/dist/ReactToastify.css';
const Navbar = () => { const Navbar = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const [user, setUser] = useState(null); 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(() => { useEffect(() => {
const loggedInUser = localStorage.getItem("user");
if (loggedInUser) {
setUser(JSON.parse(loggedInUser));
} else {
axios.get("http://localhost:8080/api/user/profile", { withCredentials: true }) axios.get("http://localhost:8080/api/user/profile", { withCredentials: true })
.then((response) => { .then((response) => {
setUser(response.data.user); 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) => { .catch((error) => {
console.error("Error fetching user data:", error); console.error("Error fetching user data:", error);
}); });
} }, [user]); // ✅ Add `user` dependency to re-run when `user` updates
}, []);
// Handle logout functionality // Handle logout functionality
const handleLogout = async () => { const handleLogout = async () => {
@@ -41,31 +80,113 @@ const Navbar = () => {
}; };
return ( return (
<header className="navbar"> <header style={{
<div className="navbar-container"> backgroundColor: "#800000",
color: "white",
padding: "15px 0",
width: "100vw",
boxShadow: "0 2px 10px rgba(0,0,0,0.1)",
position: "sticky",
top: "0",
zIndex: "1000"
}} >
<div style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
width: "95%",
margin: "0 auto"
}}>
<ToastContainer /> <ToastContainer />
<NavLink to="/Welcome" className="navbar-logo"> <NavLink to="/Welcome">
<img src="logo_.png" alt="Logo" className="logo-img" /> <img src="logo_.png" alt="Logo" style={{
height: "50px",
cursor: "pointer"
}} />
</NavLink> </NavLink>
<div className="button-container"> <div style={{ display: "flex", gap: "30px" }}>
{/* Consolidated buttons in the center */} {/* Consolidated buttons in the center */}
<NavLink to="/consolidated" className="consolidated-button nav-btn"> <NavLink to="/consolidated" style={{
color: "white",
textDecoration: "none",
fontSize: "16px",
transition: "0.3s",
padding: "10px 15px",
borderRadius: "5px"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
Faculty Consolidated Faculty Consolidated
</NavLink> </NavLink>
<NavLink to="/courseConsolidated" className="consolidated-button nav-btn"> <NavLink to="/courseConsolidated" style={{
color: "white",
textDecoration: "none",
fontSize: "16px",
transition: "0.3s",
padding: "10px 15px",
borderRadius: "5px"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
Course Consolidated Course Consolidated
</NavLink> </NavLink>
<NavLink to="/departmentConsolidated" className="navbar-title nav-btn"> <NavLink to="/departmentConsolidated" style={{
color: "white",
textDecoration: "none",
fontSize: "16px",
transition: "0.3s",
padding: "10px 15px",
borderRadius: "5px"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
Department Consolidated Department Consolidated
</NavLink> </NavLink>
<NavLink to="/panelConsolidated" style={{
color: "white",
textDecoration: "none",
fontSize: "16px",
transition: "0.3s",
padding: "10px 15px",
borderRadius: "5px"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
Panel
</NavLink>
{isAdmin && (
<NavLink to="/AdminFacultyPage" style={{
color: "white",
textDecoration: "none",
fontSize: "16px",
transition: "0.3s",
padding: "10px 15px",
borderRadius: "5px",
backgroundColor: "#B22222"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "#B22222"}>
Faculty
</NavLink>
)}
</div> </div>
<div> <div>
<button className="logout-button" onClick={handleLogout}> <button className="logout-button" onClick={handleLogout} style={{
backgroundColor: "#ff4d4d",
color: "white",
border: "none",
padding: "10px 20px",
borderRadius: "5px",
cursor: "pointer",
transition: "0.3s"
}}
onMouseEnter={(e) => e.target.style.backgroundColor = "##660000"}
onMouseLeave={(e) => e.target.style.backgroundColor = "#ff4d4d"}>
Logout Logout
</button> </button>
</div> </div>
@@ -76,15 +197,23 @@ const Navbar = () => {
<img <img
src={user.profilePicture} src={user.profilePicture}
alt="Profile" alt="Profile"
className="user-icon" style={{
style={{ width: '40px', height: '40px', borderRadius: '50%' }} width: "40px",
height: "40px",
borderRadius: "50%",
border: "2px solid white",
objectFit: "cover"
}}
/> />
) : ( ) : (
<FaUserCircle className="user-icon" /> <FaUserCircle style={{
color: "white",
fontSize: "35px"
}} />
)} )}
</NavLink> </NavLink>
</div> </div>
</header> </header >
); );
}; };

View File

@@ -16,6 +16,10 @@ const UserSchema = new mongoose.Schema(
type: Date, type: Date,
default: null, default: null,
}, },
isAdmin:{
type:Boolean,
default: false,
}
}, },
{ {
timestamps: true, timestamps: true,

View File

@@ -23,9 +23,9 @@ const transporter = nodemailer.createTransport({
pass: "umlc hbkr dpga iywd", pass: "umlc hbkr dpga iywd",
}, },
tls: { rejectUnauthorized: false }, tls: { rejectUnauthorized: false },
connectionTimeout: 30000, connectionTimeout: 60000,
greetingTimeout: 30000, greetingTimeout: 60000,
socketTimeout: 30000, socketTimeout: 60000,
}); });
// Existing Excel route unchanged, except transporter removal // Existing Excel route unchanged, except transporter removal