@opexa/portal-components 0.0.435 → 0.0.437
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { useRouter } from 'next/navigation';
|
|
3
|
-
import { useEffect, useRef } from 'react';
|
|
3
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
4
4
|
import { toaster } from '../utils/toaster.js';
|
|
5
5
|
import { useIdle } from './useIdle.js';
|
|
6
6
|
import { useSessionQuery } from './useSessionQuery.js';
|
|
@@ -9,13 +9,19 @@ const IDLE_TIMESTAMP_KEY = 'idle-logout-timestamp';
|
|
|
9
9
|
const DEFAULT_IDLE_TIMER = 1000 * 60 * 15; // 15 minutes
|
|
10
10
|
export function useIdleLogout(timer = DEFAULT_IDLE_TIMER) {
|
|
11
11
|
const router = useRouter();
|
|
12
|
-
const sessionQuery = useSessionQuery();
|
|
13
12
|
const logoutInitiated = useRef(false);
|
|
14
13
|
const isLoggingOut = useRef(false);
|
|
14
|
+
const sessionQuery = useSessionQuery();
|
|
15
|
+
const isIdle = useIdle(timer);
|
|
15
16
|
const signOutMutation = useSignOutMutation({
|
|
16
17
|
onSuccess: () => {
|
|
17
|
-
router.
|
|
18
|
-
|
|
18
|
+
router.push('/');
|
|
19
|
+
try {
|
|
20
|
+
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error('Error removing idle timestamp:', error);
|
|
24
|
+
}
|
|
19
25
|
logoutInitiated.current = false;
|
|
20
26
|
isLoggingOut.current = false;
|
|
21
27
|
},
|
|
@@ -24,41 +30,62 @@ export function useIdleLogout(timer = DEFAULT_IDLE_TIMER) {
|
|
|
24
30
|
logoutInitiated.current = false;
|
|
25
31
|
},
|
|
26
32
|
});
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
logoutInitiated.current = false;
|
|
31
|
-
isLoggingOut.current = false;
|
|
32
|
-
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
33
|
+
const handleIdleLogout = useCallback(() => {
|
|
34
|
+
if (!isIdle || logoutInitiated.current || isLoggingOut.current) {
|
|
35
|
+
return;
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (sessionQuery.data?.status === 'unauthenticated' || !isIdle || logoutInitiated.current || isLoggingOut.current) {
|
|
37
|
+
const isAuthenticated = sessionQuery.data?.status === 'authenticated';
|
|
38
|
+
if (!isAuthenticated) {
|
|
37
39
|
return;
|
|
38
40
|
}
|
|
39
|
-
|
|
41
|
+
try {
|
|
40
42
|
logoutInitiated.current = true;
|
|
41
43
|
isLoggingOut.current = true;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
toaster.warning({
|
|
44
|
+
localStorage.setItem(IDLE_TIMESTAMP_KEY, Date.now().toString());
|
|
45
|
+
signOutMutation.mutate();
|
|
46
|
+
toaster.warning({
|
|
47
|
+
description: 'You have been logged out due to inactivity.',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error('Error during idle logout:', error);
|
|
52
|
+
isLoggingOut.current = false;
|
|
53
|
+
logoutInitiated.current = false;
|
|
54
|
+
}
|
|
55
|
+
}, [isIdle, sessionQuery.data?.status, signOutMutation]);
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
handleIdleLogout();
|
|
58
|
+
}, [handleIdleLogout]);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (sessionQuery.data?.status === 'unauthenticated') {
|
|
61
|
+
logoutInitiated.current = false;
|
|
62
|
+
isLoggingOut.current = false;
|
|
45
63
|
try {
|
|
46
|
-
|
|
64
|
+
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
47
65
|
}
|
|
48
|
-
catch {
|
|
49
|
-
|
|
50
|
-
isLoggingOut.current = false;
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error('Error removing idle timestamp:', error);
|
|
51
68
|
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
}, [isIdle, sessionQuery.data?.status, signOutMutation]);
|
|
69
|
+
}
|
|
70
|
+
}, [sessionQuery.data?.status]);
|
|
55
71
|
useEffect(() => {
|
|
56
72
|
const handleStorageChange = (e) => {
|
|
57
|
-
if (e.key
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
73
|
+
if (e.key !== IDLE_TIMESTAMP_KEY || !e.newValue) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const isAuthenticated = sessionQuery.data?.status === 'authenticated';
|
|
77
|
+
if (!isAuthenticated || logoutInitiated.current) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
logoutInitiated.current = true;
|
|
82
|
+
router.push('/');
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
toaster.warning({
|
|
86
|
+
description: 'You have been logged out due to inactivity.',
|
|
87
|
+
});
|
|
88
|
+
window.location.href = '/';
|
|
62
89
|
}
|
|
63
90
|
};
|
|
64
91
|
window.addEventListener('storage', handleStorageChange);
|