File size: 902 Bytes
f8ca2a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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} />;
  };
}