import { signOut } from "next-auth/react"; import useTranslation from "next-translate/useTranslation"; import { useRouter } from "next/router"; import { destroyCookie } from "nookies"; import { useState } from "react"; import IdleTimer from "react-idle-timer"; import TimeoutModal from "./timeoutmodal"; const TimeOut = (props) => { let { t } = useTranslation(); const router = useRouter(); const { locale } = router; const [showModal, setShowModal] = useState(false); const inactiveTimeOut = 1200; // in seconds const reminderTimeout = 60; // in seconds let idleTimer = null; let logoutTimer = null; let onIdle = () => { togglePopup(); logoutTimer = setTimeout(() => { handleLogout(); }, 1000 * reminderTimeout * 1); }; let togglePopup = () => { showModal == true ? setShowModal(false) : setShowModal(true); }; let handleStayLoggedIn = () => { if (logoutTimer) { clearTimeout(logoutTimer); logoutTimer = null; } idleTimer.reset(); togglePopup(); }; const handleLogout = () => { console.info("////////////\n" + "logout" + "\n////////////"); window.localStorage.clear(), destroyCookie(null, "next-auth.csrf-token", { path: "/" }), destroyCookie(null, "next-auth.callback-url", { path: "/" }), destroyCookie(null, "pedw_locale", { path: "/" }), destroyCookie(null, "pinsUser", { path: "/" }), signOut({ callbackUrl: "/logout" }); }; return ( <> { idleTimer = ref; }} element={document} stopOnIdle={true} //onActive={console.log(" bbis active", idleTimer)} onIdle={onIdle} timeout={1000 * inactiveTimeOut * 1} /> ); }; export default TimeOut;