В основном я могу сделать:
www.MyApi.com/.autm/me => 401.
В моем случае я делаю этот вызов, и он повторяет 401, где я затем пересылаю свой интерфейс на вход в систему единого входа:
Код: Выделить всё
window.location.href = ${backendUrl}.auth/login/auth0?post_login_redirect_uri=https://myFeApp.azurestaticapps.net`
мой компонент fe:
Код: Выделить всё
const App: React.FC = () => {
resizeListener(store)
networkListener()
const user = useSelector((s: RootState) => s.user)
const [callAxiosFn, axiosState] = useAxios()
const { status, loading } = axiosState
useEffect(() => {
callAxiosFn(
{
url: loginCheckEndPoint,
method: 'GET',
},
false
)
}, [callAxiosFn])
useEffect(() => {
if (loading === false) {
if (status === 401) {
window.location.href = loginEndPoint // INFINITE LOOP HERE
}
if (status === 200) {
console.log('check if user exists and create and other')
}
}
}, [loading, status])
const isDefaultUser = JSON.stringify(user) === JSON.stringify(initalUserState)
const authResolved = loading === false && status != null
const Layout =
status === 200 && isDefaultUser
? PublicLayout
: status === 200 && !isDefaultUser
? PrivateLayout
: null
return (
{authResolved && Layout ? : null}
)
}
export default App
Код: Выделить всё
import axios, { AxiosError, AxiosRequestConfig } from 'axios'
import { useCallback, useState } from 'react'
import axiosInstance from 'utilities/requestUtillity'
export interface AxiosState {
loading: boolean | null
error: unknown
data: T | null
status: number
}
export enum MethodTypes {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE',
}
type CallAxiosFn = (config: AxiosRequestConfig, useCustomInstance?: boolean) => Promise
const defaultAxiosInstance = axios.create() // A clean Axios instance
type ResetState = () => void
type UseAxios = () => [CallAxiosFn, AxiosState, ResetState]
const useAxios: UseAxios = () => {
const [axiosState, setAxiosState] = useState({
loading: null,
error: null,
data: null,
status: 0,
})
const resetState = () => {
setAxiosState({ loading: null, error: null, data: null, status: 0 })
}
const callAxiosFn: CallAxiosFn = useCallback(async (config, useCustomInstance = true) => {
const instance = useCustomInstance ? axiosInstance : defaultAxiosInstance
// state on loading
setAxiosState({
loading: true,
error: null,
data: null,
status: 0,
})
try {
const response = await instance({
withCredentials: true, // ✅ REQUIRED for EasyAuth cookies
headers: {
// Accept: 'application/json',
...(config.headers || {}),
},
...config,
})
// state on success
setAxiosState({
loading: false,
error: null,
data: response.data,
status: response.status,
})
} catch (e) {
const axiosError = e as AxiosError
// state on error
setAxiosState({
loading: false,
error: e,
data: null,
status: axiosError.response?.status,
})
}
}, [])
return [callAxiosFn, axiosState, resetState]
}
export default useAxios
Подробнее здесь: https://stackoverflow.com/questions/798 ... fter-login