File size: 900 Bytes
8608e55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac4fbe4
8608e55
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * useLogout.js
 *
 * This custom React hook returns a memoized logout function.
 * - Sends a logout request to the backend API with the current session ID.
 * - Clears user-related state (user info, session ID, and uploaded image) on logout.
 * - Ensures the function reference is stable with useCallback for performance.
 */


import { useCallback } from "react";

export default function useLogout(sessionId, setUser, setSessionId, setImageUrl) {
  return useCallback(async () => {
    if (!sessionId) return;

    try {
      await fetch("/logout", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ session_id: sessionId }),
      });
    } catch (err) {
      console.error("Logout error:", err);
    }

    setUser(null);
    setSessionId(null);
    setImageUrl(null);
  }, [sessionId, setUser, setSessionId, setImageUrl]);
}