peppa/apps/web/app/auth/link/route.ts
2026-11-15 14:47:50 +05:00

22 lines
1.1 KiB
TypeScript

import { consumeLoginLink } from "@peppa/core";
import { NextResponse, type NextRequest } from "next/server";
import { publicOrigin, sessionCookie } from "@/lib/auth";
import { db } from "@/lib/db";
import { adoptLocale, localeCookieOptions } from "@/lib/i18n";
import { LOCALE_COOKIE, m } from "@peppa/shared/i18n";
/** One-time admin login link from `peppa web login-link`. */
export async function GET(req: NextRequest) {
const origin = await publicOrigin();
const uid = await consumeLoginLink(db, req.nextUrl.searchParams.get("code") ?? "");
const invalid = m("Ссылка входа недействительна или уже использована");
if (!uid) return NextResponse.redirect(`${origin}/login?error=${encodeURIComponent(invalid)}`);
const res = NextResponse.redirect(`${origin}/`);
const c = sessionCookie(uid);
res.cookies.set(c.name, c.value, c.options);
// PEP-98: the language picked on the login page sticks to the account (or the account's wins).
const lang = await adoptLocale(uid, req.cookies.get(LOCALE_COOKIE)?.value);
if (lang) res.cookies.set(LOCALE_COOKIE, lang, localeCookieOptions);
return res;
}