mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-15 06:18:48 +00:00
디렉터리 탐색 중 액세스 토큰이 만료됐을 때 로그인 페이지로 리다이렉션되던 문제 수정
This commit is contained in:
@@ -1,41 +1,54 @@
|
|||||||
import { signRequestBody } from "$lib/modules/crypto";
|
import { signRequestBody } from "$lib/modules/crypto";
|
||||||
|
|
||||||
export const refreshToken = async () => {
|
export const refreshToken = async (fetchInternal = fetch) => {
|
||||||
return await fetch("/api/auth/refreshToken", { method: "POST" });
|
return await fetchInternal("/api/auth/refreshToken", { method: "POST" });
|
||||||
};
|
};
|
||||||
|
|
||||||
const callApi = async (input: RequestInfo, init?: RequestInit) => {
|
const callApi = async (input: RequestInfo, init?: RequestInit, fetchInternal = fetch) => {
|
||||||
let res = await fetch(input, init);
|
let res = await fetchInternal(input, init);
|
||||||
if (res.status === 401) {
|
if (res.status === 401) {
|
||||||
res = await refreshToken();
|
res = await refreshToken();
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
res = await fetch(input, init);
|
res = await fetchInternal(input, init);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const callGetApi = async (input: RequestInfo) => {
|
export const callGetApi = async (input: RequestInfo, fetchInternal?: typeof fetch) => {
|
||||||
return await callApi(input);
|
return await callApi(input, undefined, fetchInternal);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const callPostApi = async <T>(input: RequestInfo, payload: T) => {
|
export const callPostApi = async <T>(
|
||||||
return await callApi(input, {
|
input: RequestInfo,
|
||||||
method: "POST",
|
payload: T,
|
||||||
headers: {
|
fetchInternal?: typeof fetch,
|
||||||
"Content-Type": "application/json",
|
) => {
|
||||||
|
return await callApi(
|
||||||
|
input,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(payload),
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload),
|
fetchInternal,
|
||||||
});
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const callSignedPostApi = async <T>(input: RequestInfo, payload: T, signKey: CryptoKey) => {
|
export const callSignedPostApi = async <T>(
|
||||||
return await callApi(input, {
|
input: RequestInfo,
|
||||||
method: "POST",
|
payload: T,
|
||||||
headers: {
|
signKey: CryptoKey,
|
||||||
"Content-Type": "application/json",
|
fetchInternal?: typeof fetch,
|
||||||
|
) => {
|
||||||
|
return await callApi(
|
||||||
|
input,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: await signRequestBody(payload, signKey),
|
||||||
},
|
},
|
||||||
body: await signRequestBody(payload, signKey),
|
fetchInternal,
|
||||||
});
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { error } from "@sveltejs/kit";
|
import { error } from "@sveltejs/kit";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { callGetApi } from "$lib/hooks";
|
||||||
import type { DirectroyInfoResponse, FileInfoResponse } from "$lib/server/schemas";
|
import type { DirectroyInfoResponse, FileInfoResponse } from "$lib/server/schemas";
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageLoad } from "./$types";
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params, fetch }) => {
|
export const load: PageLoad = async ({ params, fetch }) => {
|
||||||
const zodRes = z
|
const zodRes = z
|
||||||
.object({
|
.object({
|
||||||
id: z.coerce.number().int().positive().optional(),
|
id: z.coerce.number().int().positive().optional(),
|
||||||
@@ -13,13 +14,13 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
|
|||||||
const { id } = zodRes.data;
|
const { id } = zodRes.data;
|
||||||
|
|
||||||
const directoryId = id ? id : ("root" as const);
|
const directoryId = id ? id : ("root" as const);
|
||||||
const res = await fetch(`/api/directory/${directoryId}`);
|
const res = await callGetApi(`/api/directory/${directoryId}`, fetch);
|
||||||
if (!res.ok) error(404, "Not found");
|
if (!res.ok) error(404, "Not found");
|
||||||
|
|
||||||
const directoryInfo: DirectroyInfoResponse = await res.json();
|
const directoryInfo: DirectroyInfoResponse = await res.json();
|
||||||
const subDirectoryInfos = await Promise.all(
|
const subDirectoryInfos = await Promise.all(
|
||||||
directoryInfo.subDirectories.map(async (subDirectoryId) => {
|
directoryInfo.subDirectories.map(async (subDirectoryId) => {
|
||||||
const res = await fetch(`/api/directory/${subDirectoryId}`);
|
const res = await callGetApi(`/api/directory/${subDirectoryId}`, fetch);
|
||||||
if (!res.ok) error(500, "Internal server error");
|
if (!res.ok) error(500, "Internal server error");
|
||||||
return {
|
return {
|
||||||
...((await res.json()) as DirectroyInfoResponse),
|
...((await res.json()) as DirectroyInfoResponse),
|
||||||
@@ -29,7 +30,7 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
|
|||||||
);
|
);
|
||||||
const fileInfos = await Promise.all(
|
const fileInfos = await Promise.all(
|
||||||
directoryInfo.files.map(async (fileId) => {
|
directoryInfo.files.map(async (fileId) => {
|
||||||
const res = await fetch(`/api/file/${fileId}`);
|
const res = await callGetApi(`/api/file/${fileId}`, fetch);
|
||||||
if (!res.ok) error(500, "Internal server error");
|
if (!res.ok) error(500, "Internal server error");
|
||||||
return {
|
return {
|
||||||
...((await res.json()) as FileInfoResponse),
|
...((await res.json()) as FileInfoResponse),
|
||||||
Reference in New Issue
Block a user