2023-06-08 16:21:02 +02:00
|
|
|
import {
|
|
|
|
|
createPasskeyRegistrationLink,
|
|
|
|
|
getSession,
|
2023-06-13 17:43:37 +02:00
|
|
|
registerPasskey,
|
2023-06-08 16:21:02 +02:00
|
|
|
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;
|
|
|
|
|
|
2023-06-08 17:27:35 +02:00
|
|
|
const sessionCookie = await getSessionCookieById(sessionId);
|
2023-06-08 16:21:02 +02:00
|
|
|
|
2023-06-08 17:27:35 +02:00
|
|
|
const session = await getSession(
|
|
|
|
|
server,
|
|
|
|
|
sessionCookie.id,
|
|
|
|
|
sessionCookie.token
|
|
|
|
|
);
|
|
|
|
|
|
2023-06-13 17:43:37 +02:00
|
|
|
const userId = session?.session?.factors?.user?.id;
|
|
|
|
|
|
|
|
|
|
if (userId) {
|
2023-06-15 13:58:32 +02:00
|
|
|
return createPasskeyRegistrationLink(userId)
|
2023-06-08 17:27:35 +02:00
|
|
|
.then((resp) => {
|
2023-06-13 17:43:37 +02:00
|
|
|
const code = resp.code;
|
2023-06-15 13:58:32 +02:00
|
|
|
console.log("code", code);
|
2023-06-13 17:43:37 +02:00
|
|
|
return registerPasskey(userId, code).then((resp) => {
|
|
|
|
|
return NextResponse.json(resp);
|
|
|
|
|
});
|
2023-06-08 17:27:35 +02:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2023-06-15 13:58:32 +02:00
|
|
|
console.log("error on creating passkey registration link");
|
2023-06-08 17:27:35 +02:00
|
|
|
return NextResponse.json(error, { status: 500 });
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ details: "could not get session" },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-06-08 16:21:02 +02:00
|
|
|
} else {
|
2023-06-15 13:58:32 +02:00
|
|
|
return NextResponse.json({}, { status: 400 });
|
2023-06-08 16:21:02 +02:00
|
|
|
}
|
|
|
|
|
}
|