File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { useEffect } from 'react' ;
2
+ import { signOut } from 'next-auth/react' ;
3
+
4
+ const useFetchInterceptor = ( ) => {
5
+ useEffect ( ( ) => {
6
+ const handleResponse = ( response : Response ) => {
7
+ if ( response . status === 401 ) {
8
+ const currentUrl = window . location . href ;
9
+ signOut ( { callbackUrl : `/api/auth/signin?error=SessionRequired&callbackUrl=${ encodeURIComponent ( currentUrl ) } ` } ) ;
10
+ }
11
+ return response ;
12
+ } ;
13
+
14
+ const originalFetch = global . fetch . bind ( global ) ;
15
+
16
+ global . fetch = async ( input : RequestInfo | URL , init ?: RequestInit ) => {
17
+ const response = await originalFetch ( input , init ) ;
18
+ return handleResponse ( response ) ;
19
+ } ;
20
+
21
+ return ( ) => {
22
+ global . fetch = originalFetch ;
23
+ } ;
24
+ } , [ ] ) ;
25
+ } ;
26
+
27
+ export default useFetchInterceptor ;
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ import { getInitialOrchestratorConfig } from '@/configuration';
33
33
import { TranslationsProvider } from '@/translations/translationsProvider' ;
34
34
35
35
import '../font/inter.css' ;
36
+ import useFetchInterceptor from "@/hooks/useFetchInterceptor" ;
36
37
37
38
type AppOwnProps = { orchestratorConfig : OrchestratorConfig } ;
38
39
@@ -52,6 +53,7 @@ function CustomApp({
52
53
} : AppProps & AppOwnProps ) {
53
54
const router = useRouter ( ) ;
54
55
const [ queryClient ] = useState ( ( ) => new QueryClient ( queryClientConfig ) ) ;
56
+ useFetchInterceptor ( ) ;
55
57
56
58
const [ themeMode , setThemeMode ] = useState < EuiThemeColorMode > (
57
59
ColorModes . LIGHT ,
You can’t perform that action at this time.
0 commit comments