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

Logger når utloggingsvarsel trigges første gang #1542

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading