mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
web/api/session: replace writable with normal variable
This commit is contained in:
parent
a58684f314
commit
4c43a00e88
1 changed files with 3 additions and 5 deletions
|
@ -1,12 +1,11 @@
|
|||
import turnstile from "$lib/api/turnstile";
|
||||
import { writable, get } from "svelte/store";
|
||||
import { currentApiURL } from "$lib/api/api-url";
|
||||
|
||||
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`;
|
||||
|
||||
let requestHeaders = {};
|
||||
|
@ -43,7 +42,6 @@ export const requestSession = async() => {
|
|||
|
||||
export const getSession = async () => {
|
||||
const currentTime = () => Math.floor(new Date().getTime() / 1000);
|
||||
const cache = get(cachedSession);
|
||||
|
||||
if (cache?.token && cache?.exp - 2 > currentTime()) {
|
||||
return cache;
|
||||
|
@ -60,7 +58,7 @@ export const getSession = async () => {
|
|||
|
||||
if (!("status" in newSession)) {
|
||||
newSession.exp = currentTime() + newSession.exp;
|
||||
cachedSession.set(newSession);
|
||||
cache = newSession;
|
||||
}
|
||||
return newSession;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue