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 #1542 from navikt/session-error-logging
Browse files Browse the repository at this point in the history
Logger når utloggingsvarsel trigges første gang
  • Loading branch information
anders-nom authored Jun 25, 2024
2 parents 714a227 + ce13623 commit fb48606
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/utils/hooks/useLoginStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const stateSelector = (state: AppState) => ({

let timeoutId: NodeJS.Timeout | null = null;

let didLogExpiry = false;

export const useLoginStatus = () => {
const dispatch = useDispatch();
const { innloggetStatus, environment, arbeidsflate } = useSelector(stateSelector);
Expand All @@ -34,7 +36,9 @@ export const useLoginStatus = () => {
}, []);

const getExpirationInSeconds = ({ session, token }: { session: string | null; token: string | null }) => {
if (!session || !token) return { secondsToTokenExpires: null, secondsToSessionExpires: null };
if (!session || !token) {
return { secondsToTokenExpires: null, secondsToSessionExpires: null };
}

const now = new Date();
const sessionExpires = new Date(session);
Expand Down Expand Up @@ -66,13 +70,21 @@ export const useLoginStatus = () => {
window.location.href = getLogOutUrl(environment);
}

setIsTokenExpiring(_innloggetStatus.authenticated && secondsToTokenExpires < 60 * 5);
setIsSessionExpiring(secondsToSessionExpires < 60 * 10);
setSecondsToSessionExpires(secondsToSessionExpires);
const _isTokenExpiring = _innloggetStatus.authenticated && secondsToTokenExpires < 60 * 5;
const _isSessionExpiring = secondsToSessionExpires < 60 * 10;

if (secondsToTokenExpires < 0 || secondsToSessionExpires < 0) {
window.location.href = getLogOutUrl(environment);
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;
}

setIsTokenExpiring(_isTokenExpiring);
setIsSessionExpiring(_isSessionExpiring);
setSecondsToSessionExpires(secondsToSessionExpires);
};

const refreshTokenHandler = () => {
Expand Down

0 comments on commit fb48606

Please sign in to comment.