added the session countdown and added facebook logo and link

This commit was merged in pull request #6.
This commit is contained in:
Ebube
2024-02-19 23:47:10 +01:00
parent 27f63e4b80
commit 4b738aa8cd
6 changed files with 81 additions and 21 deletions
+46
View File
@@ -3,6 +3,7 @@ import {
FC,
useState,
useEffect,
useCallback,
createContext,
useContext,
Dispatch,
@@ -39,6 +40,15 @@ const useAuth = () => {
const AuthProvider: FC<WithChildren> = ({ children }) => {
const [auth, setAuth] = useState<AuthModel | undefined>(authHelper.getAuth());
const [currentUser, setCurrentUser] = useState<UserModel | undefined>();
const [lastActivityTime, setLastActivityTime] = useState(Date.now());
let checkExpirationInMinutes: number = Number(
import.meta.env.VITE_APP_SESSION_EXPIRE_MINUTES
);
let expirationChecker: number = Number(
import.meta.env.VITE_APP_SESSION_EXPIRE_CHECKER
);
const saveAuth = (auth: AuthModel | undefined) => {
setAuth(auth);
if (auth) {
@@ -53,6 +63,42 @@ const AuthProvider: FC<WithChildren> = ({ children }) => {
setCurrentUser(undefined);
};
useEffect((): any => {
const expireSession = () => {
localStorage.removeItem("wrenchboard-agent-auth-details");
logout();
};
const checkInactivity = setInterval(() => {
let currentTime: number = Date.now();
let checkLastActivityTime: number = lastActivityTime;
if (currentTime - checkLastActivityTime > checkExpirationInMinutes) {
expireSession();
}
}, expirationChecker);
// cleaning up listeners
return () => {
clearInterval(checkInactivity);
};
}, [lastActivityTime]);
// Reset last activity time on user input
const resetTime = useCallback(() => {
setLastActivityTime(Date.now());
}, []);
useEffect(() => {
window.addEventListener("mousemove", resetTime);
window.addEventListener("keydown", resetTime);
return () => {
window.removeEventListener("mousemove", resetTime);
window.removeEventListener("keydown", resetTime);
};
}, [resetTime]);
return (
<AuthContext.Provider
value={{ auth, saveAuth, currentUser, setCurrentUser, logout }}