2023-06-19 15:59:16 +02:00
|
|
|
import { getSession, server, verifyPasskeyRegistration } from "#/lib/zitadel";
|
2023-06-15 13:58:32 +02:00
|
|
|
import { getSessionCookieById } from "#/utils/cookies";
|
2023-06-19 15:59:16 +02:00
|
|
|
import { NextRequest, NextResponse, userAgent } from "next/server";
|
2023-06-15 13:58:32 +02:00
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
if (body) {
|
2023-06-19 15:59:16 +02:00
|
|
|
let { passkeyId, passkeyName, publicKeyCredential, sessionId } = body;
|
2023-06-15 13:58:32 +02:00
|
|
|
|
2023-06-19 15:59:16 +02:00
|
|
|
if (!!!passkeyName) {
|
|
|
|
|
const { browser, device, os } = userAgent(request);
|
|
|
|
|
passkeyName = `${device.vendor ?? ""} ${device.model ?? ""}${
|
|
|
|
|
device.vendor || device.model ? ", " : ""
|
|
|
|
|
}${os.name}${os.name ? ", " : ""}${browser.name}`;
|
|
|
|
|
}
|
2023-06-15 13:58:32 +02:00
|
|
|
const sessionCookie = await getSessionCookieById(sessionId);
|
|
|
|
|
|
|
|
|
|
const session = await getSession(
|
|
|
|
|
server,
|
|
|
|
|
sessionCookie.id,
|
|
|
|
|
sessionCookie.token
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const userId = session?.session?.factors?.user?.id;
|
|
|
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
|
return verifyPasskeyRegistration(
|
|
|
|
|
server,
|
|
|
|
|
passkeyId,
|
|
|
|
|
passkeyName,
|
|
|
|
|
publicKeyCredential,
|
|
|
|
|
userId
|
|
|
|
|
)
|
|
|
|
|
.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 });
|
|
|
|
|
}
|
|
|
|
|
}
|