Skip to content
Snippets Groups Projects
Select Git revision
  • 4ea99ba99e8fc9055026e93688553772a2f651d0
  • develop default
  • master protected
  • pyproject_toml
  • 3.1
  • 3.0
  • 2.15
  • 2.14
  • 2.13
  • 2.12
  • 2.11
  • 2.10
  • 2.9
  • 2.8
  • 2.6
  • 2.5
  • 2.4
  • 2.3
  • 2.2
  • 2.1
  • 2.0
  • 0.2
22 results

useFetchInterceptor.tsx

Blame
  • useFetchInterceptor.tsx 782 B
    import { useEffect } from 'react';
    import { signOut } from 'next-auth/react';
    
    const useFetchInterceptor = () => {
      useEffect(() => {
        const handleResponse = (response: Response) => {
          if (response.status === 401) {
            const currentUrl = window.location.href;
            signOut({ callbackUrl: `/api/auth/signin?error=SessionRequired&callbackUrl=${encodeURIComponent(currentUrl)}` });
          }
          return response;
        };
    
        const originalFetch = global.fetch.bind(global);
    
        global.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
          const response = await originalFetch(input, init);
          return handleResponse(response);
        };
    
        return () => {
          global.fetch = originalFetch;
        };
      }, []);
    };
    
    export default useFetchInterceptor;