20c8395c0b
- 新增 NEXT_PUBLIC_SITE_URL 支持及 site-url.ts 工具模块 - 修复 OAuth 回调域名:import.meta.env 统一读取站点 URL - 支付 API 路由新增收款地址校验 - 后端支付服务更新 - middleware 清理 - 新增 paymentSecurity 测试
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { createSupabaseRouteClient, hasSupabaseServerEnv } from "@/lib/supabase/server";
|
|
import { getConfiguredSiteUrl } from "@/lib/site-url";
|
|
|
|
function normalizeNextPath(input: string | null) {
|
|
const fallback = "/";
|
|
const raw = String(input || "").trim();
|
|
if (!raw) return fallback;
|
|
if (!raw.startsWith("/")) return fallback;
|
|
if (raw.startsWith("//")) return fallback;
|
|
return raw;
|
|
}
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const configuredSiteUrl = getConfiguredSiteUrl();
|
|
if (configuredSiteUrl) {
|
|
const canonicalOrigin = new URL(configuredSiteUrl).origin;
|
|
if (request.nextUrl.origin !== canonicalOrigin) {
|
|
const canonicalCallbackUrl = new URL(request.nextUrl.pathname, canonicalOrigin);
|
|
canonicalCallbackUrl.search = request.nextUrl.search;
|
|
return NextResponse.redirect(canonicalCallbackUrl);
|
|
}
|
|
}
|
|
|
|
const nextPath = normalizeNextPath(request.nextUrl.searchParams.get("next"));
|
|
const redirectUrl = request.nextUrl.clone();
|
|
redirectUrl.pathname = nextPath;
|
|
redirectUrl.search = "";
|
|
|
|
if (!hasSupabaseServerEnv()) {
|
|
return NextResponse.redirect(redirectUrl);
|
|
}
|
|
|
|
const response = NextResponse.redirect(redirectUrl);
|
|
const supabase = createSupabaseRouteClient(request, response);
|
|
const code = request.nextUrl.searchParams.get("code");
|
|
if (code) {
|
|
await supabase.auth.exchangeCodeForSession(code);
|
|
}
|
|
|
|
return response;
|
|
}
|