transfer cookies from old site
This commit is contained in:
gofnnp 2025-06-20 18:48:25 +04:00
parent a201f449b5
commit 665b7bad90
2 changed files with 65 additions and 23 deletions

View File

@ -7,6 +7,9 @@ export async function GET(req: NextRequest) {
const productId = req.nextUrl.searchParams.get("productId");
const placementId = req.nextUrl.searchParams.get("placementId");
const paywallId = req.nextUrl.searchParams.get("paywallId");
const fbPixels = req.nextUrl.searchParams.get("fb_pixels");
const productPrice = req.nextUrl.searchParams.get("price");
const currency = req.nextUrl.searchParams.get("currency");
const data = await createPaymentCheckout({
productId: productId || "",
@ -14,10 +17,13 @@ export async function GET(req: NextRequest) {
paywallId: paywallId || "",
});
let redirectUrl: string | URL = data?.paymentUrl;
let redirectUrl: URL = new URL(data?.paymentUrl || "", req.nextUrl.origin);
if (!redirectUrl) {
redirectUrl = new URL(`${ROUTES.paymentFailed()}`, origin);
}
if (fbPixels) redirectUrl.searchParams.set("fb_pixels", fbPixels);
if (productPrice) redirectUrl.searchParams.set("price", productPrice);
if (currency) redirectUrl.searchParams.set("currency", currency);
return NextResponse.redirect(redirectUrl, { status: 307 });
}

View File

@ -32,25 +32,61 @@ import { NextRequest, NextResponse } from "next/server";
import { ROUTES } from "@/shared/constants/client-routes";
function extractTrackingCookiesFromUrl(url: URL): Record<string, string> {
const trackingCookieKeys = [
'_fbc', '_fbp', '_ym_uid', '_ym_d', '_ym_isad', '_ym_visorc',
'yandexuid', 'ymex'
];
const cookies: Record<string, string> = {};
for (const [key, value] of url.searchParams.entries()) {
if (
trackingCookieKeys.includes(key) ||
key.startsWith('_ga') ||
key.startsWith('_gid')
) {
cookies[key] = value;
}
}
return cookies;
}
export async function GET(req: NextRequest) {
const { searchParams } = req.nextUrl;
const token = searchParams.get("jwtToken");
const productId = searchParams.get("productId");
const placementId = searchParams.get("placementId");
const paywallId = searchParams.get("paywallId");
const { searchParams } = req.nextUrl;
const token = searchParams.get("jwtToken");
const productId = searchParams.get("productId");
const placementId = searchParams.get("placementId");
const paywallId = searchParams.get("paywallId");
const fbPixels = searchParams.get("fb_pixels");
const productPrice = searchParams.get("price");
const currency = searchParams.get("currency");
const redirectUrl = new URL(`${ROUTES.payment()}`, process.env.NEXT_PUBLIC_APP_URL || "");
if (productId) redirectUrl.searchParams.set("productId", productId);
if (placementId) redirectUrl.searchParams.set("placementId", placementId);
if (paywallId) redirectUrl.searchParams.set("paywallId", paywallId);
const redirectUrl = new URL(`${ROUTES.payment()}`, process.env.NEXT_PUBLIC_APP_URL || "");
if (productId) redirectUrl.searchParams.set("productId", productId);
if (placementId) redirectUrl.searchParams.set("placementId", placementId);
if (paywallId) redirectUrl.searchParams.set("paywallId", paywallId);
if (fbPixels) redirectUrl.searchParams.set("fb_pixels", fbPixels);
if (productPrice) redirectUrl.searchParams.set("price", productPrice);
if (currency) redirectUrl.searchParams.set("currency", currency);
// Ставим куку через заголовок Set-Cookie, тк в HTML-ответе NextResponse.cookies не работает
const cookie = [
`accessToken=${encodeURIComponent(token || "")}`,
`HttpOnly; Secure; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}`,
].join("; ");
const trackingCookies = extractTrackingCookiesFromUrl(req.nextUrl);
const html = `
const cookies: string[] = [];
cookies.push(
`accessToken=${encodeURIComponent(token || "")}; HttpOnly; Secure; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}`
);
// Ставим куку через заголовок Set-Cookie, тк в HTML-ответе NextResponse.cookies не работает
Object.entries(trackingCookies).forEach(([key, value]) => {
cookies.push(
`${key}=${encodeURIComponent(value)}; HttpOnly=false; Secure=false; Path=/; SameSite=Lax; Max-Age=${60 * 60 * 24 * 365}`
);
});
const html = `
<!DOCTYPE html>
<html>
<head>
@ -65,11 +101,11 @@ export async function GET(req: NextRequest) {
</html>
`;
return new NextResponse(html, {
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
"set-cookie": cookie,
},
});
return new NextResponse(html, {
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
"set-cookie": cookies.join(", "),
},
});
}