mirror of
https://github.com/imputnet/cobalt.git
synced 2024-12-29 11:06:10 +00:00
web/api/session: replace writable with normal variable
This commit is contained in:
parent
a58684f314
commit
4c43a00e88
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue