2024-04-30 10:39:34 +02:00
|
|
|
import {
|
|
|
|
|
createPasskeyRegistrationLink,
|
|
|
|
|
getSession,
|
|
|
|
|
registerPasskey,
|
|
|
|
|
registerU2F,
|
|
|
|
|
server,
|
|
|
|
|
} from "#/lib/zitadel";
|
|
|
|
|
import { getSessionCookieById } from "#/utils/cookies";
|
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
if (body) {
|
|
|
|
|
const { sessionId } = body;
|
|
|
|
|
|
|
|
|
|
const sessionCookie = await getSessionCookieById(sessionId);
|
|
|
|
|
|
|
|
|
|
const session = await getSession(
|
|
|
|
|
server,
|
|
|
|
|
sessionCookie.id,
|
|
|
|
|
sessionCookie.token
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const domain: string = request.nextUrl.hostname;
|
|
|
|
|
|
|
|
|
|
const userId = session?.session?.factors?.user?.id;
|
|
|
|
|
|
|
|
|
|
if (userId) {
|
2024-05-03 15:07:43 +02:00
|
|
|
return registerU2F(userId, domain)
|
2024-04-30 10:39:34 +02:00
|
|
|
.then((resp) => {
|
2024-05-03 15:07:43 +02:00
|
|
|
return NextResponse.json(resp);
|
2024-04-30 10:39:34 +02:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error("error on creating passkey registration link");
|
|
|
|
|
return NextResponse.json(error, { status: 500 });
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ details: "could not get session" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return NextResponse.json({}, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
}
|