added the session countdown and added facebook logo and link
This commit was merged in pull request #6.
This commit is contained in:
@@ -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 }}
|
||||
|
||||
Reference in New Issue
Block a user