Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/app/api/affiliates/click/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { randomUUID } from "crypto";
* This is the tracking endpoint — affiliate links hit this, then redirect to the offer.
*/
export async function GET(request: NextRequest) {
const appUrl = process.env.NEXT_PUBLIC_APP_URL || "https://ugig.net";

try {
const { searchParams } = new URL(request.url);
const ref = searchParams.get("ugig_ref");

if (!ref) {
return NextResponse.redirect(new URL("/affiliates", request.url));
return NextResponse.redirect(new URL("/affiliates", appUrl));
}

const admin = createServiceClient();
Expand All @@ -32,7 +34,7 @@ export async function GET(request: NextRequest) {
.single();

if (!app) {
return NextResponse.redirect(new URL("/affiliates", request.url));
return NextResponse.redirect(new URL("/affiliates", appUrl));
}

// Read or generate a persistent visitor ID from cookie
Expand Down Expand Up @@ -67,7 +69,6 @@ export async function GET(request: NextRequest) {
}

// Add ref param to destination for client-side cookie tracking (internal URLs only)
const appUrl = process.env.NEXT_PUBLIC_APP_URL || "https://ugig.net";
const dest = new URL(redirectUrl);
if (dest.origin === new URL(appUrl).origin) {
dest.searchParams.set("ugig_ref", ref);
Expand Down Expand Up @@ -99,6 +100,7 @@ export async function GET(request: NextRequest) {
return response;
} catch (err) {
console.error("Affiliate click error:", err);
return NextResponse.redirect(new URL("/affiliates", request.url));
const appFallback = process.env.NEXT_PUBLIC_APP_URL || "https://ugig.net";
return NextResponse.redirect(new URL("/affiliates", appFallback));
}
Comment on lines 101 to 105

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The appFallback variable in the catch block is redundant — appUrl (declared on line 11, outside the try block) is already in scope here and holds the same value. Re-reading the env var creates a second source of truth for the same constant, which could cause a silent mismatch if the fallback string is ever changed in one place but not the other.

Suggested change
} catch (err) {
console.error("Affiliate click error:", err);
return NextResponse.redirect(new URL("/affiliates", request.url));
const appFallback = process.env.NEXT_PUBLIC_APP_URL || "https://ugig.net";
return NextResponse.redirect(new URL("/affiliates", appFallback));
}
} catch (err) {
console.error("Affiliate click error:", err);
return NextResponse.redirect(new URL("/affiliates", appUrl));
}

}
Loading