fix(auth): use public URL for magic-link redirects
This commit is contained in:
@@ -3,6 +3,7 @@ import { createAuthRepository, ipObservations, recordEvent } from "@minecraft-ac
|
||||
import { getClientIp } from "@minecraft-account-manager/network";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { applicationUrl } from "@/lib/application-url";
|
||||
import { db } from "@/lib/database";
|
||||
import { getIpIntelligence, toAuditIpData } from "@/lib/ip-intelligence";
|
||||
|
||||
@@ -38,7 +39,7 @@ export async function GET(request: NextRequest) {
|
||||
]);
|
||||
|
||||
const destination = result.user.firstName ? "/account" : "/welcome";
|
||||
const response = NextResponse.redirect(new URL(destination, request.url));
|
||||
const response = NextResponse.redirect(applicationUrl(destination));
|
||||
response.cookies.set(SESSION_COOKIE_NAME, result.sessionToken, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
@@ -49,7 +50,7 @@ export async function GET(request: NextRequest) {
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidLoginCodeError) {
|
||||
return NextResponse.redirect(new URL("/auth/error", request.url));
|
||||
return NextResponse.redirect(applicationUrl("/auth/error"));
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { applicationUrl } from "./application-url";
|
||||
|
||||
describe("applicationUrl", () => {
|
||||
it("builds browser redirects from the configured public application URL", () => {
|
||||
expect(applicationUrl("/welcome", "https://portal.somc.club"))
|
||||
.toEqual(new URL("https://portal.somc.club/welcome"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
export function applicationUrl(path: string, baseUrl = process.env.APP_URL) {
|
||||
if (!baseUrl) throw new Error("APP_URL is required to build public application URLs");
|
||||
return new URL(path, baseUrl);
|
||||
}
|
||||
Reference in New Issue
Block a user