Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1543 from navikt/session-error-logging-amplitude
Browse files Browse the repository at this point in the history
Logger session timeout med amplitude istedenfor console
  • Loading branch information
anders-nom authored Jun 25, 2024
2 parents fb48606 + 824264e commit 29bc7be
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/utils/hooks/useLoginStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -73,13 +75,21 @@ export const useLoginStatus = () => {
const _isTokenExpiring = _innloggetStatus.authenticated && secondsToTokenExpires < 60 * 5;
const _isSessionExpiring = secondsToSessionExpires < 60 * 10;

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;
const now = new Date();
const nowTs = now.getTime();

if ((_isTokenExpiring || _isSessionExpiring) && nowTs > lastExpireLog + logPeriodMs) {
logAmplitudeEvent('session-timeout', {
now: now.toISOString(),
nowTs,
session: _innloggetStatus.session,
token: _innloggetStatus.token,
secondsToSessionExpires,
secondsToTokenExpires,
securityLevel: _innloggetStatus.securityLevel,
});

lastExpireLog = nowTs;
}

setIsTokenExpiring(_isTokenExpiring);
Expand Down

0 comments on commit 29bc7be

Please sign in to comment.