forked from CSI-KJSCE/appointment_to_examiner
Fixed isAdmin code in navbar and login
This commit is contained in:
@@ -15,10 +15,6 @@ function AuthPage() {
|
||||
});
|
||||
const [signin, setSignin] = useState(false);
|
||||
|
||||
const adminEmails = [
|
||||
"harshitha.ss@somaiya.edu",
|
||||
];
|
||||
|
||||
const notifyError = (message) => {
|
||||
toast.error(message);
|
||||
};
|
||||
@@ -74,17 +70,8 @@ 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) {
|
||||
@@ -98,20 +85,6 @@ 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 (
|
||||
<>
|
||||
<ToastContainer />
|
||||
@@ -265,4 +238,4 @@ function SignInForm(props) {
|
||||
);
|
||||
}
|
||||
|
||||
export default AuthPage;
|
||||
export default AuthPage;
|
||||
@@ -1,6 +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 { NavLink, useNavigate } from "react-router-dom";
|
||||
import axios from "axios";
|
||||
import { toast, ToastContainer } from 'react-toastify';
|
||||
import 'react-toastify/dist/ReactToastify.css';
|
||||
@@ -10,68 +10,31 @@ const Navbar = () => {
|
||||
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(() => {
|
||||
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");
|
||||
}
|
||||
|
||||
setIsAdmin(userData.isAdmin);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error fetching user data:", error);
|
||||
});
|
||||
}, [user]); // ✅ Add `user` dependency to re-run when `user` updates
|
||||
}, []);
|
||||
|
||||
const NavlinkStyle = {
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
fontSize: "16px",
|
||||
transition: "0.3s",
|
||||
padding: "10px 15px",
|
||||
borderRadius: "5px"
|
||||
}
|
||||
|
||||
// Handle logout functionality
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
// Call the logout API
|
||||
await axios.get("http://localhost:8080/auth/logout", { withCredentials: true });
|
||||
localStorage.clear();
|
||||
|
||||
// Redirect to the login page after successful logout
|
||||
navigate("/");
|
||||
} catch (error) {
|
||||
console.error("Error during logout:", error);
|
||||
@@ -108,76 +71,37 @@ const Navbar = () => {
|
||||
|
||||
|
||||
<div style={{ display: "flex", gap: "30px" }}>
|
||||
|
||||
{/* Consolidated buttons in the center */}
|
||||
|
||||
<NavLink to="/consolidated" style={{
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
fontSize: "16px",
|
||||
transition: "0.3s",
|
||||
padding: "10px 15px",
|
||||
borderRadius: "5px"
|
||||
}}
|
||||
<NavLink to="/consolidated" style={NavlinkStyle}
|
||||
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
|
||||
Faculty Consolidated
|
||||
</NavLink>
|
||||
<NavLink to="/courseConsolidated" style={{
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
fontSize: "16px",
|
||||
transition: "0.3s",
|
||||
padding: "10px 15px",
|
||||
borderRadius: "5px"
|
||||
}}
|
||||
<NavLink to="/courseConsolidated" style={NavlinkStyle}
|
||||
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
|
||||
Course Consolidated
|
||||
</NavLink>
|
||||
<NavLink to="/departmentConsolidated" style={{
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
fontSize: "16px",
|
||||
transition: "0.3s",
|
||||
padding: "10px 15px",
|
||||
borderRadius: "5px"
|
||||
}}
|
||||
<NavLink to="/departmentConsolidated" style={NavlinkStyle}
|
||||
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
|
||||
Department Consolidated
|
||||
</NavLink>
|
||||
<NavLink to="/panelConsolidated" style={{
|
||||
color: "white",
|
||||
textDecoration: "none",
|
||||
fontSize: "16px",
|
||||
transition: "0.3s",
|
||||
padding: "10px 15px",
|
||||
borderRadius: "5px"
|
||||
}}
|
||||
<NavLink to="/panelConsolidated" style={NavlinkStyle}
|
||||
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"
|
||||
}}
|
||||
<NavLink to="/AdminFacultyPage" style={NavlinkStyle}
|
||||
onMouseEnter={(e) => e.target.style.backgroundColor = "#660000"}
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "#B22222"}>
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "transparent"}>
|
||||
Faculty
|
||||
</NavLink>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<button className="logout-button" onClick={handleLogout} style={{
|
||||
backgroundColor: "#ff4d4d",
|
||||
backgroundColor: "#660000",
|
||||
color: "white",
|
||||
border: "none",
|
||||
padding: "10px 20px",
|
||||
@@ -185,8 +109,7 @@ const Navbar = () => {
|
||||
cursor: "pointer",
|
||||
transition: "0.3s"
|
||||
}}
|
||||
onMouseEnter={(e) => e.target.style.backgroundColor = "##660000"}
|
||||
onMouseLeave={(e) => e.target.style.backgroundColor = "#ff4d4d"}>
|
||||
>
|
||||
Logout
|
||||
</button>
|
||||
</div>
|
||||
@@ -201,7 +124,7 @@ const Navbar = () => {
|
||||
width: "40px",
|
||||
height: "40px",
|
||||
borderRadius: "50%",
|
||||
border: "2px solid white",
|
||||
border: "2px solid #800000",
|
||||
objectFit: "cover"
|
||||
}}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user