peppa/apps/web/app/auth/egov/complete/route.ts
Асель 57d7857d94 PEP-71: login only through eGov, unknown IIN registers an account
Gitea and password login are gone from the web; the first eGov login creates a
Peppa account (login from the certificate name). Projects of self-registered
owners run agents only on a subscription pool, never on the machine login.

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

30 lines
1.5 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 { completeEgovFlow, isUserAllowed, PeppaError } from "@peppa/core";
import { cookies } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { EGOV_COOKIE, egovCookieOptions, safeNext, sessionCookie } from "@/lib/auth";
import { db } from "@/lib/db";
/** Finishes a signed eGov flow: logs in by the IIN (PEP-71: registers an unknown one), or links it to the current account. */
export async function POST(req: NextRequest) {
const { next } = (await req.json().catch(() => ({}))) as { next?: string };
const handle = (await cookies()).get(EGOV_COOKIE)?.value ?? "";
const done = (body: object, status = 200) => {
const res = NextResponse.json(body, { status });
res.cookies.set(EGOV_COOKIE, "", egovCookieOptions(0));
return res;
};
try {
const r = await completeEgovFlow(db, handle);
// The handle lives only in the starter's httpOnly cookie, so a link flow is finished by the
// browser (and account) that started it.
if (r.purpose === "link") return done({ redirect: "/settings?egov=linked" });
if (!(await isUserAllowed(db, r.user))) return done({ error: "Учётка отключена — обратитесь к администратору" }, 403);
const res = done({ redirect: safeNext(next) });
const c = sessionCookie(r.user.id);
res.cookies.set(c.name, c.value, c.options);
return res;
} catch (e) {
return done({ error: e instanceof PeppaError ? e.message : "Не удалось войти через eGov" }, 400);
}
}