All files / src/app/api/auth/eudi/start route.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 1/1
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40          1x                     6x 6x 6x 5x 5x 5x         5x           1x 1x            
import { NextResponse } from "next/server";
import QRCode from "qrcode";
import { startPresentation } from "@/lib/eudi-verifier";
import { newSid, newNonce, putTransaction } from "@/lib/eudi-store";
 
export const dynamic = "force-dynamic";
 
/**
 * Start an EUDI Wallet (OpenID4VP cross-device) login.
 *
 * Server-side only: initialises a presentation at the verifier, stores the
 * transaction under an opaque `sid`, and returns the wallet deep link plus a
 * pre-rendered QR data-URI. The verifier-side transaction id and nonce never
 * reach the browser.
 */
export async function POST(): Promise<NextResponse> {
  try {
    const nonce = newNonce();
    const started = await startPresentation(nonce);
    const sid = newSid();
    putTransaction({ sid, transactionId: started.transactionId, nonce });
    const qrDataUri = await QRCode.toDataURL(started.walletLink, {
      margin: 1,
      width: 320,
      errorCorrectionLevel: "M",
    });
    return NextResponse.json({
      sid,
      walletLink: started.walletLink,
      qrDataUri,
    });
  } catch (err) {
    console.error("POST /api/auth/eudi/start error:", err);
    return NextResponse.json(
      { error: "Could not start EUDI Wallet sign-in" },
      { status: 502 },
    );
  }
}