peppa/apps/web/app/auth/egov/start/route.ts
peppa-bot 324c2139b8 PEP-194: вход через eGov — повтор запроса к сервису подписи и понятная ошибка
Сервис подписи sign.aitu.uz иногда висит на одном из адресов; ответ 502
Cloudflare подменял своей страницей, и на экране входа было
«Unexpected token <… is not valid JSON».

- relay: таймаут 8 с, один повтор при сетевой ошибке или 5xx
- маршруты start/status отвечают 424 (не 5xx — Cloudflare не подменяет)
- страница входа: непонятный ответ → «eGov сейчас не отвечает…»,
  опрос статуса переживает до 5 сбоев подряд
- мок: failNext, тест повтора

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 14:22:37 +05:00

22 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PeppaError, startEgovFlow, type EgovPurpose } from "@peppa/core";
import { NextResponse, type NextRequest } from "next/server";
import QRCode from "qrcode";
import { currentUser, EGOV_COOKIE, egovCookieOptions } from "@/lib/auth";
import { db } from "@/lib/db";
import { m } from "@peppa/shared/i18n";
/** Starts an eGov QR flow (PEP-65): `login` from the login page, `link` from Settings (needs a session). */
export async function POST(req: NextRequest) {
const { purpose } = (await req.json().catch(() => ({}))) as { purpose?: EgovPurpose };
try {
const user = purpose === "link" ? await currentUser() : null;
const flow = await startEgovFlow(db, purpose === "link" ? "link" : "login", user?.id ?? null);
const qr = await QRCode.toDataURL(flow.qrContent, { errorCorrectionLevel: "M", margin: 2, width: 280 });
const res = NextResponse.json({ qr, deeplink: flow.deeplink, expiresAt: flow.expiresAt });
res.cookies.set(EGOV_COOKIE, flow.handle, egovCookieOptions(Math.ceil((flow.expiresAt - Date.now()) / 1000)));
return res;
} catch (e) {
return NextResponse.json({ error: e instanceof PeppaError ? e.message : m("Не удалось создать QR eGov") }, { status: 424 } /* not 5xx: Cloudflare replaces those with its own page (PEP-194) */);
}
}