|
|
|
@@ -1,8 +1,9 @@ |
|
|
|
import React, { createContext, useState, useEffect } from 'react'; |
|
|
|
import React, { createContext, useState, useEffect, useRef } from 'react'; |
|
|
|
import {useNavigate} from "react-router-dom"; |
|
|
|
import {useIdleTimer} from "react-idle-timer"; |
|
|
|
import { handleLogoutFunction } from 'auth/index'; |
|
|
|
import { useDispatch } from "react-redux"; |
|
|
|
import { useIntl } from "react-intl"; |
|
|
|
import { |
|
|
|
isUserLoggedIn, |
|
|
|
isGLDLoggedIn, |
|
|
|
@@ -12,9 +13,11 @@ import { |
|
|
|
const TimerContext = createContext(); |
|
|
|
|
|
|
|
const AutoLogoutProvider = ({ children }) => { |
|
|
|
const intl = useIntl(); |
|
|
|
const [lastRequestTime, setLastRequestTime] = useState(Date.now()); |
|
|
|
const navigate = useNavigate(); |
|
|
|
const [logoutInterval, setLogoutInterval] = useState(1); |
|
|
|
const idleLogoutTriggeredRef = useRef(false); |
|
|
|
// const [remainingInterval] = useState(5); |
|
|
|
const [state, setState] = useState('Active'); |
|
|
|
const dispatch = useDispatch() |
|
|
|
@@ -93,8 +96,9 @@ const AutoLogoutProvider = ({ children }) => { |
|
|
|
// console.log(remainingInterval * 60); |
|
|
|
// console.log(logoutInterval * 60 * 1000 - timeElapsed) |
|
|
|
if (timeElapsed >= logoutInterval * 60 * 1000) { |
|
|
|
if(isUserLoggedIn()){ |
|
|
|
alert("登入驗證已過期,請重新登入。") |
|
|
|
if (isUserLoggedIn() && !idleLogoutTriggeredRef.current) { |
|
|
|
idleLogoutTriggeredRef.current = true; |
|
|
|
alert(intl.formatMessage({ id: "autoLogout" })); |
|
|
|
dispatch(handleLogoutFunction()); |
|
|
|
navigate('/login'); |
|
|
|
window.location.reload(); |
|
|
|
|