Skip to content
Snippets Groups Projects
Commit 4ea99ba9 authored by Mohammad Torkashvand's avatar Mohammad Torkashvand
Browse files

Added auth interceptor to handle 401 and redirect to login with current URL

parent efbe4a27
No related branches found
No related tags found
1 merge request!33Added auth interceptor to handle 401 and redirect to login with current URL -Axios
Pipeline #87236 canceled
import { useEffect } from 'react';
import axios from 'axios';
import { signOut } from 'next-auth/react';
const useAxiosInterceptor = () => {
useEffect(() => {
const responseInterceptor = axios.interceptors.response.use(
response => response,
error => {
if (error.response && error.response.status === 401) {
const currentUrl = window.location.href;
signOut({ callbackUrl: `/api/auth/signin?error=SessionRequired&callbackUrl=${encodeURIComponent(currentUrl)}` });
}
return Promise.reject(error);
}
);
return () => {
axios.interceptors.response.eject(responseInterceptor);
};
}, []);
};
export default useAxiosInterceptor;
import { useEffect } from 'react'; import { useEffect } from 'react';
import { signOut } from 'next-auth/react'; import { signOut } from 'next-auth/react';
const useAuthInterceptor = () => { const useFetchInterceptor = () => {
useEffect(() => { useEffect(() => {
const handleResponse = (response: Response) => { const handleResponse = (response: Response) => {
if (response.status === 401) { if (response.status === 401) {
...@@ -24,4 +24,4 @@ const useAuthInterceptor = () => { ...@@ -24,4 +24,4 @@ const useAuthInterceptor = () => {
}, []); }, []);
}; };
export default useAuthInterceptor; export default useFetchInterceptor;
...@@ -3,7 +3,8 @@ import { getAppLogo } from '@/components/AppLogo/AppLogo'; ...@@ -3,7 +3,8 @@ import { getAppLogo } from '@/components/AppLogo/AppLogo';
import { WfoAuthWithPolicy } from '@/components/WfoAuthWithPolicy'; import { WfoAuthWithPolicy } from '@/components/WfoAuthWithPolicy';
import { getInitialOrchestratorConfig } from '@/configuration'; import { getInitialOrchestratorConfig } from '@/configuration';
import { GsoConfigProvider, GsoConfig } from '@/contexts/GsoConfigContext'; import { GsoConfigProvider, GsoConfig } from '@/contexts/GsoConfigContext';
import useAuthInterceptor from '@/hooks/useAuthInterceptor'; import useAxiosInterceptor from '@/hooks/useAxiosInterceptor';
import useFetchInterceptor from '@/hooks/useFetchInterceptor';
import { TranslationsProvider } from '@/translations/translationsProvider'; import { TranslationsProvider } from '@/translations/translationsProvider';
import { EuiProvider, EuiThemeColorMode } from '@elastic/eui'; import { EuiProvider, EuiThemeColorMode } from '@elastic/eui';
import '@elastic/eui/dist/eui_theme_dark.min.css'; import '@elastic/eui/dist/eui_theme_dark.min.css';
...@@ -45,6 +46,11 @@ const queryClientConfig: QueryClientConfig = { ...@@ -45,6 +46,11 @@ const queryClientConfig: QueryClientConfig = {
}, },
}; };
const useAuthInterceptor = () => {
useFetchInterceptor();
useAxiosInterceptor();
};
function CustomApp({ function CustomApp({
Component, Component,
pageProps, pageProps,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment