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 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 (
<>
<ToastContainer />