Store 초기화를 hooks.client.ts에서 수행하도록 변경

This commit is contained in:
static
2025-01-04 00:00:55 +09:00
parent aad5617d25
commit da18e6856a
5 changed files with 47 additions and 49 deletions

26
src/hooks.client.ts Normal file
View File

@@ -0,0 +1,26 @@
import type { ClientInit } from "@sveltejs/kit";
import { getClientKey, getMasterKeys } from "$lib/indexedDB";
import { clientKeyStore, masterKeyStore } from "$lib/stores";
const prepareClientKeyStore = async () => {
const [encryptKey, decryptKey, signKey, verifyKey] = await Promise.all([
getClientKey("encrypt"),
getClientKey("decrypt"),
getClientKey("sign"),
getClientKey("verify"),
]);
if (encryptKey && decryptKey && signKey && verifyKey) {
clientKeyStore.set({ encryptKey, decryptKey, signKey, verifyKey });
}
};
const prepareMasterKeyStore = async () => {
const masterKeys = await getMasterKeys();
if (masterKeys.length > 0) {
masterKeyStore.set(new Map(masterKeys.map((masterKey) => [masterKey.version, masterKey])));
}
};
export const init: ClientInit = async () => {
await Promise.all([prepareClientKeyStore(), prepareMasterKeyStore()]);
};

View File

@@ -1,4 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { goto } from "$app/navigation";
import { TitleDiv } from "$lib/components/divs";
import { clientKeyStore, masterKeyStore } from "$lib/stores";
@@ -14,17 +15,13 @@
: undefined,
);
$effect(() => {
if ($masterKeyStore) {
goto(data.redirectPath);
} else if ($clientKeyStore) {
requestMasterKeyDownload($clientKeyStore.decryptKey, $clientKeyStore.verifyKey).then(
async (ok) => {
if (ok) {
return await goto(data.redirectPath);
}
},
);
onMount(async () => {
if (
$masterKeyStore ||
($clientKeyStore &&
(await requestMasterKeyDownload($clientKeyStore.decryptKey, $clientKeyStore.verifyKey)))
) {
await goto(data.redirectPath);
}
});
</script>

View File

@@ -1,4 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { goto } from "$app/navigation";
import { Button, TextButton } from "$lib/components/buttons";
import { TitleDiv, BottomDiv } from "$lib/components/divs";
@@ -44,9 +45,9 @@
});
};
$effect(() => {
onMount(async () => {
if ($clientKeyStore) {
goto(data.redirectPath);
await goto(data.redirectPath);
}
});
</script>

View File

@@ -1,24 +1,25 @@
<script lang="ts">
import { onMount } from "svelte";
import { goto } from "$app/navigation";
import { goto as svelteGoto } from "$app/navigation";
import { clientKeyStore, masterKeyStore } from "$lib/stores";
import "../app.css";
import { prepareClientKeyStore, prepareMasterKeyStore } from "./services";
let { children } = $props();
onMount(async () => {
const redirect = async (url: string) => {
const goto = async (url: string) => {
const whitelist = ["/auth", "/key", "/client/pending"];
if (!whitelist.some((path) => location.pathname.startsWith(path))) {
await goto(`${url}?redirect=${encodeURIComponent(location.pathname + location.search)}`);
await svelteGoto(
`${url}?redirect=${encodeURIComponent(location.pathname + location.search)}`,
);
}
};
if (!(await prepareClientKeyStore())) {
return await redirect("/key/generate");
}
if (!(await prepareMasterKeyStore())) {
return await redirect("/client/pending");
if (!$clientKeyStore) {
await goto("/key/generate");
} else if (!$masterKeyStore) {
await goto("/client/pending");
}
});
</script>

View File

@@ -1,27 +0,0 @@
import { getClientKey, getMasterKeys } from "$lib/indexedDB";
import { clientKeyStore, masterKeyStore } from "$lib/stores";
export const prepareClientKeyStore = async () => {
const encryptKey = await getClientKey("encrypt");
const decryptKey = await getClientKey("decrypt");
const signKey = await getClientKey("sign");
const verifyKey = await getClientKey("verify");
if (encryptKey && decryptKey && signKey && verifyKey) {
clientKeyStore.set({ encryptKey, decryptKey, signKey, verifyKey });
return true;
} else {
return false;
}
};
export const prepareMasterKeyStore = async () => {
const masterKeys = await getMasterKeys();
if (masterKeys.length > 0) {
masterKeyStore.set(
new Map(masterKeys.map(({ version, state, key }) => [version, { version, state, key }])),
);
return true;
} else {
return false;
}
};