mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-12 21:08:46 +00:00
hook, store 리네이밍
This commit is contained in:
@@ -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<Response> => {
|
||||
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));
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
33
src/lib/hooks/gotoStateful.ts
Normal file
33
src/lib/hooks/gotoStateful.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
export { default as callAPI } from "./callAPI";
|
||||
export { goto } from "./goto";
|
||||
export { callAPI } from "./callAPI";
|
||||
export { gotoStateful } from "./gotoStateful";
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
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
2
src/lib/stores/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./auth";
|
||||
export * from "./key";
|
||||
@@ -1,4 +1,4 @@
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
export const pubKey = writable<CryptoKey | null>(null);
|
||||
export const privKey = writable<CryptoKey | null>(null);
|
||||
export const pubKeyStore = writable<CryptoKey | null>(null);
|
||||
export const privKeyStore = writable<CryptoKey | null>(null);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { Button, TextButton } from "$lib/components/buttons";
|
||||
import { TitleDiv, BottomDiv } from "$lib/components/divs";
|
||||
import { goto } from "$lib/hooks";
|
||||
import { gotoStateful } from "$lib/hooks";
|
||||
import Order from "./Order.svelte";
|
||||
import { generateKeyPair } from "./service";
|
||||
|
||||
@@ -27,7 +27,8 @@
|
||||
];
|
||||
|
||||
const generate = async () => {
|
||||
await goto("/key/export", await generateKeyPair());
|
||||
// TODO
|
||||
await gotoStateful("/key/export", await generateKeyPair());
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user