You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
46 lines
1.0 KiB
1 year ago
|
import { useEffect, useMemo } from 'react';
|
||
|
|
||
|
import useCookieJar from './useCookieJar';
|
||
|
|
||
|
const useSessionExpiryCheck = (): void => {
|
||
|
const { getSession } = useCookieJar();
|
||
|
|
||
|
// Put session in memo to avoid triggering useEffect multiple times.
|
||
|
const session = useMemo(() => getSession(), [getSession]);
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (!session) return () => null;
|
||
|
|
||
|
const { expires } = session;
|
||
|
|
||
|
const deadline = new Date(expires).getTime();
|
||
|
const nao = Date.now();
|
||
|
const diff = deadline - nao;
|
||
|
|
||
|
const tid = setTimeout(() => {
|
||
|
if (!window) return;
|
||
|
|
||
|
const { location } = window;
|
||
|
const { pathname, search } = location;
|
||
|
|
||
|
if (
|
||
|
/^\/login/.test(pathname) ||
|
||
|
(/^\/init/.test(pathname) && !search.includes('re=1'))
|
||
|
)
|
||
|
return;
|
||
|
|
||
|
location.replace('/login');
|
||
|
}, diff);
|
||
|
|
||
|
if (window) {
|
||
|
window.addEventListener('beforeunload', () => clearTimeout(tid), {
|
||
|
once: true,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
return () => clearTimeout(tid);
|
||
|
}, [session]);
|
||
|
};
|
||
|
|
||
|
export default useSessionExpiryCheck;
|