@@ -1,12 +1,10 @@ import React, { ReactElement, ReactNode } from 'react' import { Provider } from 'react-redux' import { StyledEngineProvider, ThemeProvider } from '@mui/material' -import axios from 'axios' -import * as https from 'https' import { NextPage } from 'next' import type { AppProps } from 'next/app' import { Raleway } from 'next/font/google' -import { getSession,SessionProvider } from 'next-auth/react' +import { SessionProvider } from 'next-auth/react' import { appWithTranslation } from 'next-i18next' import { NextStep, NextStepProvider } from 'nextstepjs' import { FlagProvider } from '@unleash/proxy-client-react' @@ -20,39 +18,14 @@ import { useBlockTelegram } from '#/shared/lib/hooks/use-block-telegram' import { useEnv } from '#/shared/lib/hooks/use-env' import { Providers } from '#/widgets/providers' +import { setupAxios } from '#/shared/api/axios-interceptors' + import ErrorBoundary from './error-boundary' import '#/app/styles/globals.css' import '#/app/styles/styles-pages/system.scss' -axios.defaults.httpsAgent = new https.Agent({ - rejectUnauthorized: false, -}) - -axios.defaults.validateStatus = () => true - -axios.interceptors.response.use(async (response) => { - if (![401, 404, 403].includes(response.status)) return response - - if (typeof window === 'undefined') return response - - const session = await getSession() - - if (!session) { - window.location.href = '/login' - } - - return response -}) -axios.interceptors.response.use( - (response) => response, - (error) => { - if (!error.response && typeof window !== 'undefined') { - window.location.href = '/network-error' - } - return error - } -) +setupAxios() const inter = Raleway({ subsets: ['latin'] }) @@ -0,0 +1,47 @@ +import axios from 'axios' +import * as https from 'https' +import { getSession, signOut } from 'next-auth/react' + +const BLACKLISTED_TOKEN_MESSAGE = 'Токен занесен в черный список' + +export function setupAxios() { + axios.defaults.httpsAgent = new https.Agent({ + rejectUnauthorized: false, + }) + + axios.defaults.validateStatus = () => true + + axios.interceptors.response.use(async (response) => { + if (typeof window === 'undefined') return response + + + if (response.status === 401) { + await signOut({ callbackUrl: '/login' }) + return response + } + + if (![401, 404, 403].includes(response.status)) return response + + const session = await getSession() + + if (!session || session.error) { + await signOut({ callbackUrl: '/login' }) + } + + return response + }) + + axios.interceptors.response.use( + (response) => response, + (error) => { + if (!error.response && typeof window !== 'undefined') { + window.location.href = '/network-error' + } + + if (error.response.status === 401) { + signOut({ callbackUrl: '/login' }) + } + return error + } + ) +}