nexora / frontend /src /components /shared /Security /withAdminProtection.js
ChandimaPrabath's picture
dashboard update 0.1
f8ca2a5
import { useState, useEffect } from 'react';
import PasswordPrompt from '@/components/shared/Security/PasswordPrompt';
import Cookies from 'js-cookie';
export default function withAdminProtection(Component) {
return function ProtectedComponent(props) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => {
const checkAuth = async () => {
const res = await fetch('/api/post/check-auth', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
if (res.ok) {
setIsAuthenticated(true);
}
};
checkAuth();
}, []);
const handleAuthenticated = () => {
setIsAuthenticated(true);
};
if (!isAuthenticated) {
return <PasswordPrompt onAuthenticated={handleAuthenticated} />;
}
return <Component {...props} />;
};
}