web/api/session: replace writable with normal variable

This commit is contained in:
jj 2024-10-20 11:46:09 +00:00
parent a58684f314
commit 4c43a00e88
No known key found for this signature in database

View file

@ -1,12 +1,11 @@
import turnstile from "$lib/api/turnstile"; import turnstile from "$lib/api/turnstile";
import { writable, get } from "svelte/store";
import { currentApiURL } from "$lib/api/api-url"; import { currentApiURL } from "$lib/api/api-url";
import type { CobaltSession, CobaltErrorResponse, CobaltSessionResponse } from "$lib/types/api"; import type { CobaltSession, CobaltErrorResponse, CobaltSessionResponse } from "$lib/types/api";
const cachedSession = writable<CobaltSession | undefined>(); let cache: CobaltSession | undefined;
export const requestSession = async() => { export const requestSession = async () => {
const apiEndpoint = `${currentApiURL()}/session`; const apiEndpoint = `${currentApiURL()}/session`;
let requestHeaders = {}; let requestHeaders = {};
@ -43,7 +42,6 @@ export const requestSession = async() => {
export const getSession = async () => { export const getSession = async () => {
const currentTime = () => Math.floor(new Date().getTime() / 1000); const currentTime = () => Math.floor(new Date().getTime() / 1000);
const cache = get(cachedSession);
if (cache?.token && cache?.exp - 2 > currentTime()) { if (cache?.token && cache?.exp - 2 > currentTime()) {
return cache; return cache;
@ -60,7 +58,7 @@ export const getSession = async () => {
if (!("status" in newSession)) { if (!("status" in newSession)) {
newSession.exp = currentTime() + newSession.exp; newSession.exp = currentTime() + newSession.exp;
cachedSession.set(newSession); cache = newSession;
} }
return newSession; return newSession;
} }