From 6ff486522c232c7e89eacdcdba67a0fb3895abd0 Mon Sep 17 00:00:00 2001 From: Anders Nomerstad Date: Tue, 25 Jun 2024 16:52:47 +0200 Subject: [PATCH 1/2] Logger session timeout med amplitude istedenfor console --- src/utils/hooks/useLoginStatus.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/utils/hooks/useLoginStatus.ts b/src/utils/hooks/useLoginStatus.ts index 567e9c135..3c028e57a 100644 --- a/src/utils/hooks/useLoginStatus.ts +++ b/src/utils/hooks/useLoginStatus.ts @@ -4,6 +4,7 @@ import { AppState } from 'store/reducers'; import { hentInnloggingsstatus, fornyInnlogging } from 'store/reducers/innloggingsstatus-duck'; import { getLogOutUrl, getLoginUrl } from 'utils/login'; import { useLoginDebug } from './useLoginDebug'; +import { logAmplitudeEvent } from '../analytics/amplitude'; const stateSelector = (state: AppState) => ({ innloggetStatus: state.innloggingsstatus.data, @@ -13,7 +14,8 @@ const stateSelector = (state: AppState) => ({ let timeoutId: NodeJS.Timeout | null = null; -let didLogExpiry = false; +const logPeriodMs = 60000; +let lastExpireLog = 0; export const useLoginStatus = () => { const dispatch = useDispatch(); @@ -72,14 +74,19 @@ export const useLoginStatus = () => { const _isTokenExpiring = _innloggetStatus.authenticated && secondsToTokenExpires < 60 * 5; const _isSessionExpiring = secondsToSessionExpires < 60 * 10; + const now = new Date(); - if ((_isTokenExpiring || _isSessionExpiring) && !didLogExpiry) { - console.error( - `Session is timing out - Now: ${new Date().toISOString()} - Session: ${secondsToSessionExpires} / ${ - _innloggetStatus.session.endsAt - } - Token: ${secondsToTokenExpires} / ${_innloggetStatus.token.endsAt}` - ); - didLogExpiry = true; + if ((_isTokenExpiring || _isSessionExpiring) && now.getTime() > lastExpireLog + logPeriodMs) { + logAmplitudeEvent('session-timeout', { + now: now.toISOString(), + session: _innloggetStatus.session, + token: _innloggetStatus.token, + secondsToSessionExpires, + secondsToTokenExpires, + securityLevel: _innloggetStatus.securityLevel, + }); + + lastExpireLog = now.getTime(); } setIsTokenExpiring(_isTokenExpiring); From 824264e3582739a05750ea4e846bf7c3843ae0d1 Mon Sep 17 00:00:00 2001 From: Anders Nomerstad Date: Tue, 25 Jun 2024 16:54:33 +0200 Subject: [PATCH 2/2] Logger getTime --- src/utils/hooks/useLoginStatus.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utils/hooks/useLoginStatus.ts b/src/utils/hooks/useLoginStatus.ts index 3c028e57a..6e1f39431 100644 --- a/src/utils/hooks/useLoginStatus.ts +++ b/src/utils/hooks/useLoginStatus.ts @@ -74,11 +74,14 @@ export const useLoginStatus = () => { const _isTokenExpiring = _innloggetStatus.authenticated && secondsToTokenExpires < 60 * 5; const _isSessionExpiring = secondsToSessionExpires < 60 * 10; + const now = new Date(); + const nowTs = now.getTime(); - if ((_isTokenExpiring || _isSessionExpiring) && now.getTime() > lastExpireLog + logPeriodMs) { + if ((_isTokenExpiring || _isSessionExpiring) && nowTs > lastExpireLog + logPeriodMs) { logAmplitudeEvent('session-timeout', { now: now.toISOString(), + nowTs, session: _innloggetStatus.session, token: _innloggetStatus.token, secondsToSessionExpires, @@ -86,7 +89,7 @@ export const useLoginStatus = () => { securityLevel: _innloggetStatus.securityLevel, }); - lastExpireLog = now.getTime(); + lastExpireLog = nowTs; } setIsTokenExpiring(_isTokenExpiring);