mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-02-04 08:06:56 +00:00
/api/auth 아래의 Endpoint들을 tRPC로 마이그레이션
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { callPostApi } from "$lib/hooks";
|
||||
import type { PasswordChangeRequest } from "$lib/server/schemas";
|
||||
import { useTRPC } from "$trpc/client";
|
||||
|
||||
export const requestPasswordChange = async (oldPassword: string, newPassword: string) => {
|
||||
const res = await callPostApi<PasswordChangeRequest>("/api/auth/changePassword", {
|
||||
oldPassword,
|
||||
newPassword,
|
||||
});
|
||||
return res.ok;
|
||||
const trpc = useTRPC();
|
||||
|
||||
try {
|
||||
await trpc.auth.changePassword.mutate({ oldPassword, newPassword });
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { callPostApi } from "$lib/hooks";
|
||||
import type { LoginRequest } from "$lib/server/schemas";
|
||||
import { useTRPC } from "$trpc/client";
|
||||
|
||||
export { requestLogout } from "$lib/services/auth";
|
||||
export { requestDeletedFilesCleanup } from "$lib/services/file";
|
||||
@@ -9,6 +8,13 @@ export {
|
||||
} from "$lib/services/key";
|
||||
|
||||
export const requestLogin = async (email: string, password: string) => {
|
||||
const res = await callPostApi<LoginRequest>("/api/auth/login", { email, password });
|
||||
return res.ok;
|
||||
const trpc = useTRPC();
|
||||
|
||||
try {
|
||||
await trpc.auth.login.mutate({ email, password });
|
||||
return true;
|
||||
} catch {
|
||||
// TODO: Error Handling
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { error, text } from "@sveltejs/kit";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { passwordChangeRequest } from "$lib/server/schemas";
|
||||
import { changePassword } from "$lib/server/services/auth";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, request }) => {
|
||||
const { sessionId, userId } = await authorize(locals, "any");
|
||||
|
||||
const zodRes = passwordChangeRequest.safeParse(await request.json());
|
||||
if (!zodRes.success) error(400, "Invalid request body");
|
||||
const { oldPassword, newPassword } = zodRes.data;
|
||||
|
||||
await changePassword(userId, sessionId, oldPassword, newPassword);
|
||||
return text("Password changed", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
@@ -1,21 +0,0 @@
|
||||
import { error, text } from "@sveltejs/kit";
|
||||
import env from "$lib/server/loadenv";
|
||||
import { loginRequest } from "$lib/server/schemas";
|
||||
import { login } from "$lib/server/services/auth";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, request, cookies }) => {
|
||||
const zodRes = loginRequest.safeParse(await request.json());
|
||||
if (!zodRes.success) error(400, "Invalid request body");
|
||||
const { email, password } = zodRes.data;
|
||||
|
||||
const { sessionIdSigned } = await login(email, password, locals.ip, locals.userAgent);
|
||||
cookies.set("sessionId", sessionIdSigned, {
|
||||
path: "/",
|
||||
maxAge: env.session.exp / 1000,
|
||||
secure: true,
|
||||
sameSite: "strict",
|
||||
});
|
||||
|
||||
return text("Logged in", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
import { text } from "@sveltejs/kit";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { logout } from "$lib/server/services/auth";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, cookies }) => {
|
||||
const { sessionId } = await authorize(locals, "any");
|
||||
|
||||
await logout(sessionId);
|
||||
cookies.delete("sessionId", { path: "/" });
|
||||
|
||||
return text("Logged out", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
import { error, json } from "@sveltejs/kit";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import {
|
||||
sessionUpgradeRequest,
|
||||
sessionUpgradeResponse,
|
||||
type SessionUpgradeResponse,
|
||||
} from "$lib/server/schemas";
|
||||
import { createSessionUpgradeChallenge } from "$lib/server/services/auth";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, request }) => {
|
||||
const { sessionId, userId } = await authorize(locals, "notClient");
|
||||
|
||||
const zodRes = sessionUpgradeRequest.safeParse(await request.json());
|
||||
if (!zodRes.success) error(400, "Invalid request body");
|
||||
const { encPubKey, sigPubKey } = zodRes.data;
|
||||
|
||||
const { id, challenge } = await createSessionUpgradeChallenge(
|
||||
sessionId,
|
||||
userId,
|
||||
locals.ip,
|
||||
encPubKey,
|
||||
sigPubKey,
|
||||
);
|
||||
return json(sessionUpgradeResponse.parse({ id, challenge } satisfies SessionUpgradeResponse));
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
import { error, text } from "@sveltejs/kit";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { sessionUpgradeVerifyRequest } from "$lib/server/schemas";
|
||||
import { verifySessionUpgradeChallenge } from "$lib/server/services/auth";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, request }) => {
|
||||
const { sessionId, userId } = await authorize(locals, "notClient");
|
||||
|
||||
const zodRes = sessionUpgradeVerifyRequest.safeParse(await request.json());
|
||||
if (!zodRes.success) error(400, "Invalid request body");
|
||||
const { id, answerSig, force } = zodRes.data;
|
||||
|
||||
await verifySessionUpgradeChallenge(sessionId, userId, locals.ip, id, answerSig, force);
|
||||
return text("Session upgraded", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
Reference in New Issue
Block a user