Files
Checkmate/Client/src/HOC/withAdminCheck.jsx
T

31 lines
906 B
React
Raw Normal View History

2024-07-19 10:24:01 -07:00
import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import axiosInstance from "../Utils/axiosConfig";
import { logger } from "../Utils/Logger";
2024-07-19 10:24:01 -07:00
const withAdminCheck = (WrappedComponent) => {
const WithAdminCheck = (props) => {
const navigate = useNavigate();
useEffect(() => {
axiosInstance
.get("/auth/users/admin")
.then((response) => {
if (response.data.data === true) {
navigate("/login");
}
})
.catch((error) => {
logger.error(error);
2024-07-19 10:24:01 -07:00
});
}, [navigate]);
2024-07-19 10:29:43 -07:00
return <WrappedComponent {...props} isAdmin={true} />;
2024-07-19 10:24:01 -07:00
};
const wrappedComponentName =
WrappedComponent.displayName || WrappedComponent.name || "Component";
WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`;
return WithAdminCheck;
};
export default withAdminCheck;