diff --git a/src/app/api/auth/callback/route.ts b/src/app/api/auth/callback/route.ts index 062c4d4..011848d 100644 --- a/src/app/api/auth/callback/route.ts +++ b/src/app/api/auth/callback/route.ts @@ -5,7 +5,7 @@ import { getOrigin } from "@/lib/origin/server"; export async function GET(req: NextRequest) { const code = req.nextUrl.searchParams.get("code"); - const { protocol, host } = await getOrigin(); + const { httpOrigin } = await getOrigin(); if (!code) { return NextResponse.json({ error: "code param required" }, { status: 400 }); @@ -20,5 +20,5 @@ export async function GET(req: NextRequest) { return NextResponse.json({ error: error.message }, { status: 500 }); } - return NextResponse.redirect(`${protocol}://${host}/problems`); + return NextResponse.redirect(`${httpOrigin}/problems`); } diff --git a/src/app/runs/[runId]/components/Editor.tsx b/src/app/runs/[runId]/components/Editor.tsx index ea60513..9bc9a77 100644 --- a/src/app/runs/[runId]/components/Editor.tsx +++ b/src/app/runs/[runId]/components/Editor.tsx @@ -56,9 +56,9 @@ export default function Editor({ // WebSocket connection useEffect(() => { - if (!run || !origin?.host) return; + if (!run || !origin?.wsOrigin) return; - const ws = new WebSocket(`ws://${origin.host}/api/view-run`); + const ws = new WebSocket(`${origin.wsOrigin}/api/view-run`); ws.onopen = () => { ws.send(run.id); @@ -100,7 +100,7 @@ export default function Editor({ }; return () => ws.close(); - }, [run, ]); + }, [run]); return ( { - const h = await headers(); - const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000"; - const protocol = h.get("x-forwarded-proto") || h.get(":scheme") || "https"; - - return { protocol, host }; +export async function getOrigin() { + return Core.getOrigin(); } diff --git a/src/lib/origin/server.ts b/src/lib/origin/server.ts index 90e63f4..76f023a 100644 --- a/src/lib/origin/server.ts +++ b/src/lib/origin/server.ts @@ -1,7 +1,14 @@ -import { headers } from "next/headers"; import "server-only"; -export async function getOrigin(): Promise<{ protocol: string; host: string }> { +import { headers } from "next/headers"; + +export async function getOrigin(): Promise<{ + protocol: string; + host: string; + wsProtocol: string; + httpOrigin: string; + wsOrigin: string; +}> { const h = await headers(); const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000"; const protocol = @@ -10,5 +17,13 @@ export async function getOrigin(): Promise<{ protocol: string; host: string }> { h.get(":scheme") || "https"; - return { protocol, host }; + const wsProtocol = protocol === "https" ? "wss" : "ws"; + + return { + protocol, + host, + wsProtocol, + httpOrigin: `${protocol}://${host}`, + wsOrigin: `${wsProtocol}://${host}`, + }; } diff --git a/src/modules/account/actions.ts b/src/modules/account/actions.ts index 47fac86..5afafb5 100644 --- a/src/modules/account/actions.ts +++ b/src/modules/account/actions.ts @@ -8,12 +8,12 @@ import { redirect } from "next/navigation"; export async function signInWithGitHub() { const supabase = await createClient(); - const { host, protocol } = await getOrigin(); + const { httpOrigin } = await getOrigin(); const { data, error } = await supabase.auth.signInWithOAuth({ provider: "github", options: { - redirectTo: `${protocol}://${host}/api/auth/callback`, + redirectTo: `${httpOrigin}/api/auth/callback`, }, });