|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { signOut, useSession } from "next-auth/react"; |
| 4 | +import { useIdleTimer } from "react-idle-timer"; |
| 5 | +import type { ModalProps, ModalRef } from "../../designSystem/modal/Modal"; |
| 6 | +import { useEffect, useRef, useState } from "react"; |
| 7 | +import dynamic from "next/dynamic"; |
| 8 | +import { PAGES } from "@/app/shared/page-routes"; |
| 9 | + |
| 10 | +const Modal = dynamic<ModalProps>( |
| 11 | + () => import("../../designSystem/modal/Modal").then((mod) => mod.Modal), |
| 12 | + { ssr: false }, |
| 13 | +); |
| 14 | + |
| 15 | +// 60 mins inactivity |
| 16 | +export const IDLE_TIMEOUT_MSEC = 60 * 60000; |
| 17 | +// 5 mins to answer prompt / 5 mins before timeout |
| 18 | +export const PROMPT_TIMEOUT_MSEC = 5 * 60000; |
| 19 | + |
| 20 | +export interface SessionTimeoutProps { |
| 21 | + idleTimeMsec: number; |
| 22 | + promptTimeMsec: number; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @param root0 Props of SessionTimeout component |
| 27 | + * @param root0.idleTimeMsec amout of time that the user needs to be idle in order to trigger automatic signout |
| 28 | + * @param root0.promptTimeMsec amount of time that the prompt will be displayed to a user. This time uses as reference the idle timeout |
| 29 | + * for example if it is set to 5 mins then the prompt will become visible 5 mins before the idle time has been reached. |
| 30 | + * @returns SessionTimeout component which handles tracking idle time for a logged in user. |
| 31 | + */ |
| 32 | +const SessionTimeout: React.FC<SessionTimeoutProps> = ({ |
| 33 | + idleTimeMsec, |
| 34 | + promptTimeMsec, |
| 35 | +}) => { |
| 36 | + const { status } = useSession(); |
| 37 | + const [remainingTime, setRemainingTime] = useState(""); |
| 38 | + const intervalId = useRef<NodeJS.Timeout | null>(null); |
| 39 | + const [started, setStarted] = useState(false); |
| 40 | + const modalRef = useRef<ModalRef>(null); |
| 41 | + |
| 42 | + const { activate, start, reset, pause, getRemainingTime } = useIdleTimer({ |
| 43 | + timeout: idleTimeMsec, |
| 44 | + promptBeforeIdle: promptTimeMsec, |
| 45 | + onPrompt: handlePrompt, |
| 46 | + onIdle: handleLogout, |
| 47 | + stopOnIdle: true, |
| 48 | + startManually: true, |
| 49 | + disabled: status !== "authenticated", |
| 50 | + }); |
| 51 | + |
| 52 | + function handlePrompt() { |
| 53 | + intervalId.current = setInterval(() => { |
| 54 | + const msecs = getRemainingTime(); |
| 55 | + const mins = Math.floor(msecs / 60000); |
| 56 | + const secs = Math.floor((msecs / 1000) % 60); |
| 57 | + |
| 58 | + setRemainingTime( |
| 59 | + `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`, |
| 60 | + ); |
| 61 | + }, 500); |
| 62 | + |
| 63 | + modalRef.current?.toggleModal(); |
| 64 | + } |
| 65 | + |
| 66 | + async function handleLogout() { |
| 67 | + if (intervalId.current !== null) { |
| 68 | + clearInterval(intervalId.current); |
| 69 | + } |
| 70 | + |
| 71 | + modalRef.current?.modalIsOpen && modalRef.current?.toggleModal(); |
| 72 | + //stop timer and logout |
| 73 | + reset(); |
| 74 | + pause(); |
| 75 | + await signOut({ redirectTo: PAGES.LANDING }); |
| 76 | + } |
| 77 | + |
| 78 | + function handleStay() { |
| 79 | + modalRef.current?.modalIsOpen && modalRef.current?.toggleModal(); |
| 80 | + activate(); |
| 81 | + } |
| 82 | + |
| 83 | + // Inititalize timer |
| 84 | + useEffect(() => { |
| 85 | + if (!started && status === "authenticated") { |
| 86 | + setStarted(true); |
| 87 | + start(); |
| 88 | + } |
| 89 | + }, [status, started, setStarted, start]); |
| 90 | + |
| 91 | + return ( |
| 92 | + <Modal |
| 93 | + id="session-timeout" |
| 94 | + modalRef={modalRef} |
| 95 | + heading={`Your session will end in ${remainingTime}`} |
| 96 | + forceAction |
| 97 | + className={"width-auto padding-x-3"} |
| 98 | + buttons={[ |
| 99 | + { |
| 100 | + text: "Yes, stay signed in", |
| 101 | + type: "button" as const, |
| 102 | + id: "session-timeout-stayin", |
| 103 | + className: "usa-button", |
| 104 | + onClick: handleStay, |
| 105 | + }, |
| 106 | + { |
| 107 | + text: "Sign out", |
| 108 | + type: "button" as const, |
| 109 | + id: "session-timeout-signout", |
| 110 | + className: "usa-button usa-button--outline", |
| 111 | + onClick: handleLogout, |
| 112 | + }, |
| 113 | + ]} |
| 114 | + > |
| 115 | + <p> |
| 116 | + You've been inactive for over 55 minutes. |
| 117 | + <br /> |
| 118 | + Do you wish to stay signed in? |
| 119 | + </p> |
| 120 | + </Modal> |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export default SessionTimeout; |
0 commit comments