@opexa/portal-components 0.0.434 → 0.0.436
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.
|
@@ -14,68 +14,58 @@ export function useIdleLogout(timer = DEFAULT_IDLE_TIMER) {
|
|
|
14
14
|
const isLoggingOut = useRef(false);
|
|
15
15
|
const signOutMutation = useSignOutMutation({
|
|
16
16
|
onSuccess: () => {
|
|
17
|
-
router.
|
|
17
|
+
router.push("/");
|
|
18
18
|
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
19
|
+
logoutInitiated.current = false;
|
|
20
|
+
isLoggingOut.current = false;
|
|
19
21
|
},
|
|
20
22
|
onError: () => {
|
|
21
23
|
isLoggingOut.current = false;
|
|
24
|
+
logoutInitiated.current = false;
|
|
22
25
|
},
|
|
23
26
|
});
|
|
24
27
|
const isIdle = useIdle(timer);
|
|
25
|
-
const sessionStatusRef = useRef(sessionQuery.data?.status);
|
|
26
|
-
sessionStatusRef.current = sessionQuery.data?.status;
|
|
27
28
|
useEffect(() => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
if (sessionQuery.data?.status === 'authenticated') {
|
|
34
|
-
const lastActivityTimestamp = localStorage.getItem(IDLE_TIMESTAMP_KEY);
|
|
35
|
-
if (lastActivityTimestamp) {
|
|
36
|
-
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
37
|
-
const timeElapsed = Date.now() - parseInt(lastActivityTimestamp, 10);
|
|
38
|
-
if (timeElapsed > timer) {
|
|
39
|
-
if (!logoutInitiated.current) {
|
|
40
|
-
logoutInitiated.current = true;
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
toaster.warning({
|
|
43
|
-
title: 'You have been away for a while.',
|
|
44
|
-
description: 'You have been logged out for inactivity.',
|
|
45
|
-
});
|
|
46
|
-
isLoggingOut.current = true;
|
|
47
|
-
signOutMutation.mutate();
|
|
48
|
-
}, 0);
|
|
49
|
-
}
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
window.addEventListener('beforeunload', handleUnload);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
29
|
+
if (sessionQuery.data?.status === 'unauthenticated') {
|
|
30
|
+
logoutInitiated.current = false;
|
|
31
|
+
isLoggingOut.current = false;
|
|
56
32
|
localStorage.removeItem(IDLE_TIMESTAMP_KEY);
|
|
57
33
|
}
|
|
58
|
-
|
|
59
|
-
window.removeEventListener('beforeunload', handleUnload);
|
|
60
|
-
};
|
|
61
|
-
}, [sessionQuery.data?.status, signOutMutation, timer]);
|
|
34
|
+
}, [sessionQuery.data?.status]);
|
|
62
35
|
useEffect(() => {
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
logoutInitiated.current = true;
|
|
68
|
-
setTimeout(() => {
|
|
69
|
-
toaster.warning({
|
|
70
|
-
title: 'You have been idle for a while.',
|
|
71
|
-
description: 'You will be logged out for inactivity.',
|
|
72
|
-
});
|
|
73
|
-
isLoggingOut.current = true;
|
|
74
|
-
signOutMutation.mutate();
|
|
75
|
-
}, 0);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
logoutInitiated.current = false;
|
|
36
|
+
if (sessionQuery.data?.status === 'unauthenticated' || !isIdle || logoutInitiated.current || isLoggingOut.current) {
|
|
37
|
+
return;
|
|
79
38
|
}
|
|
39
|
+
const handleIdleLogout = async () => {
|
|
40
|
+
logoutInitiated.current = true;
|
|
41
|
+
isLoggingOut.current = true;
|
|
42
|
+
const timestamp = Date.now().toString();
|
|
43
|
+
localStorage.setItem(IDLE_TIMESTAMP_KEY, timestamp);
|
|
44
|
+
toaster.warning({ description: 'You have been logged out due to inactivity.' });
|
|
45
|
+
try {
|
|
46
|
+
await signOutMutation.mutateAsync();
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
logoutInitiated.current = false;
|
|
50
|
+
isLoggingOut.current = false;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
handleIdleLogout();
|
|
80
54
|
}, [isIdle, sessionQuery.data?.status, signOutMutation]);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const handleStorageChange = (e) => {
|
|
57
|
+
if (e.key === IDLE_TIMESTAMP_KEY && e.newValue && sessionQuery.data?.status === 'authenticated') {
|
|
58
|
+
if (!logoutInitiated.current) {
|
|
59
|
+
logoutInitiated.current = true;
|
|
60
|
+
router.push('/');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
window.addEventListener('storage', handleStorageChange);
|
|
65
|
+
return () => window.removeEventListener('storage', handleStorageChange);
|
|
66
|
+
}, [router, sessionQuery.data?.status]);
|
|
67
|
+
return {
|
|
68
|
+
isIdle,
|
|
69
|
+
isLoggingOut: isLoggingOut.current,
|
|
70
|
+
};
|
|
81
71
|
}
|
|
@@ -83,7 +83,7 @@ export function SignUpDefaultForm() {
|
|
|
83
83
|
}),
|
|
84
84
|
firstName: z
|
|
85
85
|
.string()
|
|
86
|
-
.regex(/^[a-
|
|
86
|
+
.regex(/^[a-z ]+$/gi, 'First name must contain only letters')
|
|
87
87
|
.transform((val) => val.trim())
|
|
88
88
|
.refine((val) => val.length >= 2, {
|
|
89
89
|
message: 'First name must be 2 or more characters',
|
|
@@ -93,7 +93,7 @@ export function SignUpDefaultForm() {
|
|
|
93
93
|
}),
|
|
94
94
|
lastName: z
|
|
95
95
|
.string()
|
|
96
|
-
.regex(/^[a-
|
|
96
|
+
.regex(/^[a-z ]+$/gi, 'Last name must contain only letters')
|
|
97
97
|
.transform((val) => val.trim())
|
|
98
98
|
.refine((val) => val.length >= 2, {
|
|
99
99
|
message: 'Last name must be 2 or more characters',
|