diff --git a/src/lib/hooks/callAPI.ts b/src/lib/hooks/callAPI.ts index 60311b1..73df085 100644 --- a/src/lib/hooks/callAPI.ts +++ b/src/lib/hooks/callAPI.ts @@ -1,29 +1,30 @@ -import { accessToken } from "$lib/stores/auth"; +import { get } from "svelte/store"; +import { accessTokenStore } from "$lib/stores"; const refreshToken = async () => { - const res = await fetch("/api/auth/refreshtoken", { + const res = await fetch("/api/auth/refreshToken", { method: "POST", credentials: "same-origin", }); if (!res.ok) { - accessToken.set(null); + accessTokenStore.set(null); throw new Error("Failed to refresh token"); } const data = await res.json(); const token = data.accessToken as string; - accessToken.set(token); + accessTokenStore.set(token); return token; }; const callAPIInternal = async ( input: RequestInfo, - init?: RequestInit, - token?: string | null, + init: RequestInit | undefined, + token: string | null, retryIfUnauthorized = true, ): Promise => { - if (token === null) { + if (!token) { token = await refreshToken(); retryIfUnauthorized = false; } @@ -35,13 +36,13 @@ const callAPIInternal = async ( Authorization: `Bearer ${token}`, }, }); - if (res.status === 401 && retryIfUnauthorized && token !== undefined) { + if (res.status === 401 && retryIfUnauthorized) { return await callAPIInternal(input, init, null, false); } return res; }; -export default async (input: RequestInfo, init?: RequestInit, token?: string | null) => { - return await callAPIInternal(input, init, token); +export const callAPI = async (input: RequestInfo, init?: RequestInit) => { + return await callAPIInternal(input, init, get(accessTokenStore)); }; diff --git a/src/lib/hooks/goto.ts b/src/lib/hooks/goto.ts deleted file mode 100644 index c2cef9c..0000000 --- a/src/lib/hooks/goto.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { writable } from "svelte/store"; -import { goto as svelteGoto } from "$app/navigation"; - -type Path = "/key/export"; - -interface KeyExportState { - pubKeyBase64: string; - privKeyBase64: string; -} - -export const keyExportState = writable(null); - -export function goto(path: "/key/export", state: KeyExportState): Promise; - -export function goto(path: Path, state: unknown) { - switch (path) { - case "/key/export": - keyExportState.set(state as KeyExportState); - return svelteGoto(path); - } -} diff --git a/src/lib/hooks/gotoStateful.ts b/src/lib/hooks/gotoStateful.ts new file mode 100644 index 0000000..1a05294 --- /dev/null +++ b/src/lib/hooks/gotoStateful.ts @@ -0,0 +1,33 @@ +import { goto } from "$app/navigation"; + +type Path = "/key/export"; + +interface KeyExportState { + pubKeyBase64: string; + privKeyBase64: string; +} + +const useAutoNull = (value: T | null) => { + return { + get: () => { + const result = value; + value = null; + return result; + }, + set: (newValue: T) => { + value = newValue; + }, + }; +}; + +export const keyExportState = useAutoNull(null); + +export function gotoStateful(path: "/key/export", state: KeyExportState): Promise; + +export function gotoStateful(path: Path, state: unknown) { + switch (path) { + case "/key/export": + keyExportState.set(state as KeyExportState); + return goto(path); + } +} diff --git a/src/lib/hooks/index.ts b/src/lib/hooks/index.ts index 48c19b9..e2f0392 100644 --- a/src/lib/hooks/index.ts +++ b/src/lib/hooks/index.ts @@ -1,2 +1,2 @@ -export { default as callAPI } from "./callAPI"; -export { goto } from "./goto"; +export { callAPI } from "./callAPI"; +export { gotoStateful } from "./gotoStateful"; diff --git a/src/lib/stores/auth.ts b/src/lib/stores/auth.ts index 954cee0..93e276c 100644 --- a/src/lib/stores/auth.ts +++ b/src/lib/stores/auth.ts @@ -1,3 +1,3 @@ import { writable } from "svelte/store"; -export const accessToken = writable(null); +export const accessTokenStore = writable(null); diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts new file mode 100644 index 0000000..86b0be9 --- /dev/null +++ b/src/lib/stores/index.ts @@ -0,0 +1,2 @@ +export * from "./auth"; +export * from "./key"; diff --git a/src/lib/stores/key.ts b/src/lib/stores/key.ts index e11209c..40458bb 100644 --- a/src/lib/stores/key.ts +++ b/src/lib/stores/key.ts @@ -1,4 +1,4 @@ import { writable } from "svelte/store"; -export const pubKey = writable(null); -export const privKey = writable(null); +export const pubKeyStore = writable(null); +export const privKeyStore = writable(null); diff --git a/src/routes/(fullscreen)/auth/login/service.ts b/src/routes/(fullscreen)/auth/login/service.ts index 4799ea5..f88dbc8 100644 --- a/src/routes/(fullscreen)/auth/login/service.ts +++ b/src/routes/(fullscreen)/auth/login/service.ts @@ -1,5 +1,5 @@ import { callAPI } from "$lib/hooks"; -import { accessToken } from "$lib/stores/auth"; +import { accessTokenStore } from "$lib/stores"; export const requestLogin = async (email: string, password: string) => { const res = await callAPI("/api/auth/login", { @@ -16,6 +16,6 @@ export const requestLogin = async (email: string, password: string) => { const data = await res.json(); const token = data.accessToken as string; - accessToken.set(token); + accessTokenStore.set(token); return true; }; diff --git a/src/routes/(fullscreen)/key/export/+page.ts b/src/routes/(fullscreen)/key/export/+page.ts index 994640b..a64ea53 100644 --- a/src/routes/(fullscreen)/key/export/+page.ts +++ b/src/routes/(fullscreen)/key/export/+page.ts @@ -1,14 +1,11 @@ import { error } from "@sveltejs/kit"; -import { get } from "svelte/store"; -import { keyExportState } from "$lib/hooks/goto"; +import { keyExportState } from "$lib/hooks/gotoStateful"; import type { PageLoad } from "./$types"; export const load: PageLoad = async () => { - const state = get(keyExportState); + const state = keyExportState.get(); if (!state) { error(403, "Forbidden"); } - - keyExportState.set(null); return state; }; diff --git a/src/routes/(fullscreen)/key/generate/+page.svelte b/src/routes/(fullscreen)/key/generate/+page.svelte index eddcf85..af84aa8 100644 --- a/src/routes/(fullscreen)/key/generate/+page.svelte +++ b/src/routes/(fullscreen)/key/generate/+page.svelte @@ -1,7 +1,7 @@ diff --git a/src/routes/(fullscreen)/key/generate/service.ts b/src/routes/(fullscreen)/key/generate/service.ts index aa46c87..7b869d4 100644 --- a/src/routes/(fullscreen)/key/generate/service.ts +++ b/src/routes/(fullscreen)/key/generate/service.ts @@ -1,5 +1,5 @@ import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB"; -import { pubKey, privKey } from "$lib/stores/key"; +import { pubKeyStore, privKeyStore } from "$lib/stores"; type KeyType = "public" | "private"; @@ -44,8 +44,8 @@ export const generateKeyPair = async () => { const keyPair = await generateRSAKeyPair(); const privKeySecure = await makeRSAKeyNonextractable(keyPair.privateKey, "private"); - pubKey.set(keyPair.publicKey); - privKey.set(privKeySecure); + pubKeyStore.set(keyPair.publicKey); + privKeyStore.set(privKeySecure); await storeKeyPairIntoIndexedDB(keyPair.publicKey, privKeySecure);