암호 키 등록 후 Refresh Token 업그레이드 구현

This commit is contained in:
static
2024-12-29 01:55:01 +09:00
parent af51f04b94
commit 516375142d
7 changed files with 77 additions and 4 deletions

View File

@@ -0,0 +1,29 @@
import { error, text } from "@sveltejs/kit";
import { z } from "zod";
import { upgradeTokens } from "$lib/server/services/auth";
import type { RequestHandler } from "./$types";
export const POST: RequestHandler = async ({ request, cookies }) => {
const token = cookies.get("refreshToken");
if (!token) error(401, "Refresh token not found");
const zodRes = z
.object({
pubKey: z.string().base64().nonempty(),
})
.safeParse(await request.json());
if (!zodRes.success) error(400, "Invalid request body");
const { pubKey } = zodRes.data;
const { accessToken, refreshToken } = await upgradeTokens(token.trim(), pubKey.trim());
cookies.set("accessToken", accessToken, {
path: "/",
sameSite: "strict",
});
cookies.set("refreshToken", refreshToken, {
path: "/api/auth",
sameSite: "strict",
});
return text("Token upgraded", { headers: { "Content-Type": "text/plain" } });
};