diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
| commit | b7e39b59fd0fc7b5610ebff29035bf622079e0d8 (patch) | |
| tree | 64be84ebbdac9f7ceced983390c53b10d575af5c /apps/kit/src/lib/session.ts | |
| parent | 2001c035fbb417ab0a3d42cfb04d17420bde4086 (diff) | |
| download | greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.tar.xz greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.zip | |
refactor: Change file structure
Diffstat (limited to 'apps/kit/src/lib/session.ts')
| -rw-r--r-- | apps/kit/src/lib/session.ts | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/apps/kit/src/lib/session.ts b/apps/kit/src/lib/session.ts deleted file mode 100644 index ee79933..0000000 --- a/apps/kit/src/lib/session.ts +++ /dev/null @@ -1,69 +0,0 @@ -import {logError, logInfo} from "$lib/logger"; -import { Temporal } from "temporal-polyfill"; -import { get_profile_for_active_check, logout } from "./api/user"; -import { is_guid, session_storage_get_json, session_storage_set_json } from "./helpers"; -import { SECONDS_BETWEEN_SESSION_CHECK, StorageKeys } from "./configuration"; -import type { ISession } from "$lib/models/ISession"; - -export async function is_active(forceRefresh: boolean = false): Promise<boolean> { - const nowEpoch = Temporal.Now.instant().epochSeconds; - const data = session_storage_get_json(StorageKeys.session) as ISession; - const expiryEpoch = data?.lastChecked + SECONDS_BETWEEN_SESSION_CHECK; - const lastCheckIsStaleOrNone = !is_guid(data?.profile?.id) || (expiryEpoch < nowEpoch); - if (forceRefresh || lastCheckIsStaleOrNone) { - return await call_api(); - } else { - const sessionIsValid = data.profile && is_guid(data.profile.id); - if (!sessionIsValid) { - clear_session_data(); - logInfo("Session data is not valid"); - } - return sessionIsValid; - } -} - -export async function end_session(cb: Function): Promise<void> { - await logout(); - clear_session_data(); - cb(); -} - -async function call_api(): Promise<boolean> { - logInfo("Getting profile data while checking session state"); - try { - const response = await get_profile_for_active_check(); - if (response.ok) { - const userData = await response.data; - if (is_guid(userData.id) && userData.username) { - const session = { - profile: userData, - lastChecked: Temporal.Now.instant().epochSeconds - } as ISession; - session_storage_set_json(StorageKeys.session, session); - logInfo("Successfully got profile data while checking session state"); - return true; - } else { - logError("Api returned invalid data while getting profile data"); - clear_session_data(); - return false; - } - } else { - logError("Api returned unsuccessfully while getting profile data"); - clear_session_data(); - return false; - } - } catch (e) { - logError(e); - clear_session_data(); - return false; - } -} - -export function clear_session_data() { - session_storage_set_json(StorageKeys.session, {}); - logInfo("Cleared session data."); -} - -export function get_session_data(): ISession { - return session_storage_get_json(StorageKeys.session) as ISession; -} |
