hook, store 리네이밍

This commit is contained in:
static
2024-12-27 21:50:04 +09:00
parent 400438c395
commit 1aafe126d6
11 changed files with 61 additions and 48 deletions

View File

@@ -1,29 +1,30 @@
import { accessToken } from "$lib/stores/auth"; import { get } from "svelte/store";
import { accessTokenStore } from "$lib/stores";
const refreshToken = async () => { const refreshToken = async () => {
const res = await fetch("/api/auth/refreshtoken", { const res = await fetch("/api/auth/refreshToken", {
method: "POST", method: "POST",
credentials: "same-origin", credentials: "same-origin",
}); });
if (!res.ok) { if (!res.ok) {
accessToken.set(null); accessTokenStore.set(null);
throw new Error("Failed to refresh token"); throw new Error("Failed to refresh token");
} }
const data = await res.json(); const data = await res.json();
const token = data.accessToken as string; const token = data.accessToken as string;
accessToken.set(token); accessTokenStore.set(token);
return token; return token;
}; };
const callAPIInternal = async ( const callAPIInternal = async (
input: RequestInfo, input: RequestInfo,
init?: RequestInit, init: RequestInit | undefined,
token?: string | null, token: string | null,
retryIfUnauthorized = true, retryIfUnauthorized = true,
): Promise<Response> => { ): Promise<Response> => {
if (token === null) { if (!token) {
token = await refreshToken(); token = await refreshToken();
retryIfUnauthorized = false; retryIfUnauthorized = false;
} }
@@ -35,13 +36,13 @@ const callAPIInternal = async (
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
}); });
if (res.status === 401 && retryIfUnauthorized && token !== undefined) { if (res.status === 401 && retryIfUnauthorized) {
return await callAPIInternal(input, init, null, false); return await callAPIInternal(input, init, null, false);
} }
return res; return res;
}; };
export default async (input: RequestInfo, init?: RequestInit, token?: string | null) => { export const callAPI = async (input: RequestInfo, init?: RequestInit) => {
return await callAPIInternal(input, init, token); return await callAPIInternal(input, init, get(accessTokenStore));
}; };

View File

@@ -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<KeyExportState | null>(null);
export function goto(path: "/key/export", state: KeyExportState): Promise<void>;
export function goto(path: Path, state: unknown) {
switch (path) {
case "/key/export":
keyExportState.set(state as KeyExportState);
return svelteGoto(path);
}
}

View File

@@ -0,0 +1,33 @@
import { goto } from "$app/navigation";
type Path = "/key/export";
interface KeyExportState {
pubKeyBase64: string;
privKeyBase64: string;
}
const useAutoNull = <T>(value: T | null) => {
return {
get: () => {
const result = value;
value = null;
return result;
},
set: (newValue: T) => {
value = newValue;
},
};
};
export const keyExportState = useAutoNull<KeyExportState>(null);
export function gotoStateful(path: "/key/export", state: KeyExportState): Promise<void>;
export function gotoStateful(path: Path, state: unknown) {
switch (path) {
case "/key/export":
keyExportState.set(state as KeyExportState);
return goto(path);
}
}

View File

@@ -1,2 +1,2 @@
export { default as callAPI } from "./callAPI"; export { callAPI } from "./callAPI";
export { goto } from "./goto"; export { gotoStateful } from "./gotoStateful";

View File

@@ -1,3 +1,3 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store";
export const accessToken = writable<string | null>(null); export const accessTokenStore = writable<string | null>(null);

2
src/lib/stores/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from "./auth";
export * from "./key";

View File

@@ -1,4 +1,4 @@
import { writable } from "svelte/store"; import { writable } from "svelte/store";
export const pubKey = writable<CryptoKey | null>(null); export const pubKeyStore = writable<CryptoKey | null>(null);
export const privKey = writable<CryptoKey | null>(null); export const privKeyStore = writable<CryptoKey | null>(null);

View File

@@ -1,5 +1,5 @@
import { callAPI } from "$lib/hooks"; import { callAPI } from "$lib/hooks";
import { accessToken } from "$lib/stores/auth"; import { accessTokenStore } from "$lib/stores";
export const requestLogin = async (email: string, password: string) => { export const requestLogin = async (email: string, password: string) => {
const res = await callAPI("/api/auth/login", { 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 data = await res.json();
const token = data.accessToken as string; const token = data.accessToken as string;
accessToken.set(token); accessTokenStore.set(token);
return true; return true;
}; };

View File

@@ -1,14 +1,11 @@
import { error } from "@sveltejs/kit"; import { error } from "@sveltejs/kit";
import { get } from "svelte/store"; import { keyExportState } from "$lib/hooks/gotoStateful";
import { keyExportState } from "$lib/hooks/goto";
import type { PageLoad } from "./$types"; import type { PageLoad } from "./$types";
export const load: PageLoad = async () => { export const load: PageLoad = async () => {
const state = get(keyExportState); const state = keyExportState.get();
if (!state) { if (!state) {
error(403, "Forbidden"); error(403, "Forbidden");
} }
keyExportState.set(null);
return state; return state;
}; };

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { Button, TextButton } from "$lib/components/buttons"; import { Button, TextButton } from "$lib/components/buttons";
import { TitleDiv, BottomDiv } from "$lib/components/divs"; import { TitleDiv, BottomDiv } from "$lib/components/divs";
import { goto } from "$lib/hooks"; import { gotoStateful } from "$lib/hooks";
import Order from "./Order.svelte"; import Order from "./Order.svelte";
import { generateKeyPair } from "./service"; import { generateKeyPair } from "./service";
@@ -27,7 +27,8 @@
]; ];
const generate = async () => { const generate = async () => {
await goto("/key/export", await generateKeyPair()); // TODO
await gotoStateful("/key/export", await generateKeyPair());
}; };
</script> </script>

View File

@@ -1,5 +1,5 @@
import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB"; import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB";
import { pubKey, privKey } from "$lib/stores/key"; import { pubKeyStore, privKeyStore } from "$lib/stores";
type KeyType = "public" | "private"; type KeyType = "public" | "private";
@@ -44,8 +44,8 @@ export const generateKeyPair = async () => {
const keyPair = await generateRSAKeyPair(); const keyPair = await generateRSAKeyPair();
const privKeySecure = await makeRSAKeyNonextractable(keyPair.privateKey, "private"); const privKeySecure = await makeRSAKeyNonextractable(keyPair.privateKey, "private");
pubKey.set(keyPair.publicKey); pubKeyStore.set(keyPair.publicKey);
privKey.set(privKeySecure); privKeyStore.set(privKeySecure);
await storeKeyPairIntoIndexedDB(keyPair.publicKey, privKeySecure); await storeKeyPairIntoIndexedDB(keyPair.publicKey, privKeySecure);