From 13646a1f1b3145a190fbc2bb860702df2817aa97 Mon Sep 17 00:00:00 2001 From: tone Date: Thu, 18 Dec 2025 22:00:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=89=8D=E7=AB=AF=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=80=9A=E8=A1=8C=E8=AF=81API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontend/lib/api/endpoints/auth.client.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/apps/frontend/lib/api/endpoints/auth.client.ts b/apps/frontend/lib/api/endpoints/auth.client.ts index 3a58511..95161fa 100644 --- a/apps/frontend/lib/api/endpoints/auth.client.ts +++ b/apps/frontend/lib/api/endpoints/auth.client.ts @@ -1,6 +1,7 @@ import { User } from "@/lib/types/user"; import { clientFetch } from "../client"; import { APIError } from "../common"; +import { PublicKeyCredentialCreationOptionsJSON, RegistrationResponseJSON } from "@simplewebauthn/browser"; export async function loginByPassword(identifier: string, password: string) { identifier = identifier.trim(); @@ -53,4 +54,42 @@ export async function loginBySms(phone: string, code: string) { export async function logout() { return clientFetch('/api/auth/logout', { method: 'POST' }); +} + + +// ======== PassKey ======== +export async function getPasskeyRegisterOptions() { + return clientFetch('/api/auth/passkey/register/options', { + method: 'POST', + }); +} + +export async function passkeyRegister(name: string, credentialResponse: RegistrationResponseJSON) { + name = name.trim(); + if (name.length === 0) { + throw new APIError('通行证名称不得为空'); + } + + return clientFetch<{ id: string; name: string; createdAt: string }>('/api/auth/passkey/register', { + method: 'POST', + body: JSON.stringify({ + name, + credentialResponse, + }) + }); +} + +export async function getLoginByPasskeyOptions() { + return clientFetch('/api/auth/passkey/login/options', { + method: 'POST', + }) +} + +export async function loginByPasskey(credentialResponse: any) { + return clientFetch('/api/auth/passkey/login', { + method: 'POST', + body: JSON.stringify({ + credentialResponse, + }) + }) } \ No newline at end of file