mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-12 07:42:24 +00:00
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { getSession, server, verifyU2FRegistration } from "#/lib/zitadel";
|
|
import { getSessionCookieById } from "#/utils/cookies";
|
|
import { VerifyU2FRegistrationRequest } from "@zitadel/server";
|
|
import { NextRequest, NextResponse, userAgent } from "next/server";
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const body = await request.json();
|
|
if (body) {
|
|
let { u2fId, passkeyName, publicKeyCredential, sessionId } = body;
|
|
|
|
if (!!!passkeyName) {
|
|
const { browser, device, os } = userAgent(request);
|
|
passkeyName = `${device.vendor ?? ""} ${device.model ?? ""}${
|
|
device.vendor || device.model ? ", " : ""
|
|
}${os.name}${os.name ? ", " : ""}${browser.name}`;
|
|
}
|
|
const sessionCookie = await getSessionCookieById(sessionId);
|
|
|
|
const session = await getSession(
|
|
server,
|
|
sessionCookie.id,
|
|
sessionCookie.token
|
|
);
|
|
|
|
const userId = session?.session?.factors?.user?.id;
|
|
|
|
if (userId) {
|
|
const req: VerifyU2FRegistrationRequest = {
|
|
publicKeyCredential,
|
|
u2fId,
|
|
userId,
|
|
tokenName: passkeyName,
|
|
};
|
|
return verifyU2FRegistration(req)
|
|
.then((resp) => {
|
|
return NextResponse.json(resp);
|
|
})
|
|
.catch((error) => {
|
|
return NextResponse.json(error, { status: 500 });
|
|
});
|
|
} else {
|
|
return NextResponse.json(
|
|
{ details: "could not get session" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} else {
|
|
return NextResponse.json({}, { status: 400 });
|
|
}
|
|
}
|