Skip to content

Commit

Permalink
Forbedre utloggingsvarsel (#350)
Browse files Browse the repository at this point in the history
Beregner tid for utloggingsvarsel basert på `expires_in_seconds` og
regner om dette til lokal tid. Dermed slipper vi problemer hvor brukere
har feil klokkeslett på maskinen sin.

Rydder i logout-warning for bedre lesbarhet.

Skriver enhetstester for auth-hjelperen.

Flytter også logout-warning ut av controller-mappe (som den eneste
komponenten der) og legger den i src sammen med de andre.

Foreslår å bumpe vitest fordi gammel versjon ikke fungerer med VS-code.

---------

Co-authored-by: Andreas Nordahl <[email protected]>
  • Loading branch information
terjeofnorway and andnorda authored Jul 3, 2024
1 parent a8c63cd commit bd2d031
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 266 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
"@types/lodash.debounce": "4.0.9",
"autoprefixer": "10.4.14",
"jsdom": "24.0.0",
"msw": "2.3.0",
"postcss": "8.4.27",
"postcss-prefix-selector": "1.16.0",
"rollup-plugin-minify-html-literals-v3": "1.3.3",
"vite-bundle-visualizer": "1.2.1",
"vite-tsconfig-paths": "4.2.0",
"vitest": "1.3.1"
"vitest": "1.6.0",
"msw": "2.3.0"
}
}
207 changes: 0 additions & 207 deletions packages/client/src/controllers/logout-warning.ts

This file was deleted.

43 changes: 43 additions & 0 deletions packages/client/src/helpers/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { SessionData, transformSessionToAuth } from "./auth";

describe("Auth helpers", () => {
describe("transformSessionToAuth", () => {
it("correctly uses expire_in_seconds when transforming to local time", () => {
const mockCurrentISODate = "2021-10-10T10:10:00.000Z";

// Note: Discrepancy between ISO dates and ends_in_seconds and expire_in_seconds
// This is intentional to test that the function does infact use the seconds and not the time stamps.
const mockSessionData: SessionData = {
session: {
created_at: "2021-10-10T10:00:00.000Z",
ends_at: "2021-10-10T16:00:00.000Z",
timeout_at: "2021-10-10T16:00:00.000Z",
ends_in_seconds: 3600,
active: true,
timeout_in_seconds: 3600,
},
tokens: {
expire_at: "2021-10-10T11:05:00.000Z",
refreshed_at: "2021-10-10T12:05:00.000Z",
expire_in_seconds: 1800,
next_auto_refresh_in_seconds: 1800,
refresh_cooldown: false,
refresh_cooldown_seconds: 0,
},
};

vi.useFakeTimers();
vi.setSystemTime(mockCurrentISODate);

const authData = transformSessionToAuth(mockSessionData);

expect(authData.sessionExpireAtLocal).toBe(
"2021-10-10T11:10:00.000Z",
);
expect(authData.tokenExpireAtLocal).toBe(
"2021-10-10T10:40:00.000Z",
);
});
});
});
41 changes: 19 additions & 22 deletions packages/client/src/helpers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type AuthData = {
export type SessionData = {
session: {
created_at: string;
ends_at: string;
Expand All @@ -25,14 +25,9 @@ export async function fetchSession() {
credentials: "include",
});

return await sessionResponse.json();
return (await sessionResponse.json()) as SessionData;
} catch (error) {
console.log(`User is not logged in`);
return {
authenticated: false,
name: "",
securityLevel: "",
};
return null;
}
}

Expand All @@ -44,23 +39,25 @@ export async function fetchRenew() {
credentials: "include",
});

return await sessionResponse.json();
return (await sessionResponse.json()) as SessionData;
} catch (error) {
console.log(`User is not logged in`);
return {
authenticated: false,
name: "",
securityLevel: "",
};
return null;
}
}

export function getSecondsToExpiration(expiration: string) {
const now = new Date().getTime();
const expires = new Date(expiration).getTime();
return Math.ceil((expires - now) / 1000);
}
export function transformSessionToAuth(session: SessionData) {
const sessionExpireInSeconds = session.session.ends_in_seconds;
const tokenExpireInSeconds = session.tokens.expire_in_seconds;

const sessionExpireAtLocal = new Date(
new Date().getTime() + sessionExpireInSeconds * 1000,
).toISOString();
const tokenExpireAtLocal = new Date(
new Date().getTime() + tokenExpireInSeconds * 1000,
).toISOString();

export function fakeExpirationTime(seconds: number) {
return new Date(Date.now() + seconds * 1000).toISOString();
return {
sessionExpireAtLocal,
tokenExpireAtLocal,
};
}
Loading

0 comments on commit bd2d031

Please sign in to comment.