|
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
|
|
Dispatch,
|
|
|
|
|
SetStateAction,
|
|
|
|
|
} from 'react'
|
|
|
|
|
import {useLocation} from 'react-router-dom'
|
|
|
|
|
import {LayoutSplashScreen} from '../../../../_res/layout/core'
|
|
|
|
|
import {AuthModel, UserModel} from './_models'
|
|
|
|
|
import * as authHelper from './AuthHelpers'
|
|
|
|
@@ -61,11 +62,17 @@ const AuthProvider: FC<WithChildren> = ({children}) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AuthInit: FC<WithChildren> = ({children}) => {
|
|
|
|
|
const pathname = useLocation().pathname
|
|
|
|
|
const {auth, logout, setCurrentUser} = useAuth()
|
|
|
|
|
const didRequest = useRef(false)
|
|
|
|
|
const [showSplashScreen, setShowSplashScreen] = useState(true)
|
|
|
|
|
// We should request user by authToken (IN OUR EXAMPLE IT'S API_TOKEN) before rendering the application
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// function to expire session after 5 mins
|
|
|
|
|
let logoutSession = setTimeout(()=>{ //expire session after 5 mins
|
|
|
|
|
logout()
|
|
|
|
|
}, Number(process.env.REACT_APP_SESSION_TIMEOUT))
|
|
|
|
|
|
|
|
|
|
const requestUser = async (apiToken: string) => {
|
|
|
|
|
try {
|
|
|
|
|
if (!didRequest.current) {
|
|
|
|
@@ -96,7 +103,11 @@ const AuthInit: FC<WithChildren> = ({children}) => {
|
|
|
|
|
setShowSplashScreen(false)
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
return ()=>{ // clears session timeout side effect
|
|
|
|
|
clearInterval(logoutSession)
|
|
|
|
|
}
|
|
|
|
|
}, [pathname])
|
|
|
|
|
|
|
|
|
|
return showSplashScreen ? <LayoutSplashScreen /> : <>{children}</>
|
|
|
|
|
}
|
|
|
|
|