File size: 987 Bytes
f8b5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from "react";
import CommunityHub from "@/models/communityHub";

/**
 * Hook to check if the user is authenticated with the community hub by checking
 * the user defined connection key in the settings.
 * @returns {{isAuthenticated: boolean, loading: boolean}} An object containing the authentication status and loading state.
 */
export function useCommunityHubAuth() {
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    async function checkCommunityHubAuth() {
      setLoading(true);
      try {
        const { connectionKey } = await CommunityHub.getSettings();
        setIsAuthenticated(!!connectionKey);
      } catch (error) {
        console.error("Error checking hub auth:", error);
        setIsAuthenticated(false);
      } finally {
        setLoading(false);
      }
    }
    checkCommunityHubAuth();
  }, []);

  return { isAuthenticated, loading };
}