47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import {
|
|
COOKIE_NAME,
|
|
getCookieOptions,
|
|
getHostFromRequest,
|
|
} from "@/app/lib/auth-cookie";
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const body = await request.json().catch(() => ({}));
|
|
const token = body?.token;
|
|
const record = body?.record;
|
|
const anonymousUserId =
|
|
typeof body?.anonymousUserId === "string" ? body.anonymousUserId.trim() : "";
|
|
|
|
if (!token || !record?.id) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: "缺少 token 或 record" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const payload: Record<string, string> = {
|
|
token,
|
|
userId: record.id,
|
|
email: record.email ?? "",
|
|
};
|
|
|
|
if (/^user\d+$/.test(anonymousUserId)) {
|
|
payload.anonymousUserId = anonymousUserId;
|
|
}
|
|
|
|
const encoded = Buffer.from(JSON.stringify(payload), "utf-8").toString(
|
|
"base64url"
|
|
);
|
|
|
|
const res = NextResponse.json({ ok: true });
|
|
res.cookies.set(
|
|
COOKIE_NAME,
|
|
encoded,
|
|
getCookieOptions(false, getHostFromRequest(request)) as Record<
|
|
string,
|
|
string | number | boolean
|
|
>
|
|
);
|
|
return res;
|
|
}
|