MCPcopy Create free account
hub / github.com/WardAnalytics/WardGraph / useAuthState

Function useAuthState

src/hooks/useAuthState.tsx:10–39  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8 * @returns An object containing the user, and whether the user is authenticated
9 */
10const useAuthState = () => {
11 // Get user from local storage
12 const localUser = localStorage.getItem("user");
13 const initialUser = localUser ? JSON.parse(localUser) : null;
14
15 const [user, setUser] = useState<User | null>(initialUser);
16 const [isAuthenticated, setIsAuthenticated] = useState<boolean>(
17 initialUser?.emailVerified || false,
18 );
19 const [isLoading, setIsLoading] = useState(true);
20
21 useEffect(() => {
22 // This listener will be called whenever the user's sign-in state changes
23 const unsubscribe = auth.onAuthStateChanged((currentUser) => {
24 setUser(currentUser);
25 setIsLoading(false);
26 localStorage.setItem("user", JSON.stringify(currentUser));
27 });
28
29 // Cleanup subscription on unmount
30 return () => unsubscribe();
31 }, []); // Empty array ensures this effect runs only once on mount
32
33 useEffect(() => {
34 setIsAuthenticated(user?.emailVerified || false);
35 localStorage.setItem("user", JSON.stringify(user));
36 }, [user]);
37
38 return { user, isAuthenticated, isLoading };
39};
40
41export default useAuthState;

Callers 15

AppFunction · 0.85
WithAuthFunction · 0.85
DeleteGraphDialogFunction · 0.85
CreateGraphDialogFunction · 0.85
GraphProvidedFunction · 0.85
ShowTutorialPopupFunction · 0.85
SearchBarFunction · 0.85
NewAddressModalFunction · 0.85
LabelsAndTagsFunction · 0.85
ExpandWithAIFunction · 0.85
AddressNodeFunction · 0.85
TurnPremiumDialogFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected