diff --git a/src/app/runs/[runId]/components/Editor.tsx b/src/app/runs/[runId]/components/Editor.tsx index b3478bf..31e59d7 100644 --- a/src/app/runs/[runId]/components/Editor.tsx +++ b/src/app/runs/[runId]/components/Editor.tsx @@ -10,6 +10,8 @@ import { python } from "@codemirror/lang-python"; import { java } from "@codemirror/lang-java"; import { useEffect, useRef, useState } from "react"; import { LiveRun, LiveRunEvent, LiveRunMove } from "@/modules/live-run/types"; +import useAction from "@/hook/use-action"; +import { getOrigin } from "@/modules/getOrigin"; type Language = | "javascript" @@ -46,6 +48,7 @@ export default function Editor({ run: LiveRun | null | undefined; onEvent: (s: LiveRunEvent) => void; }) { + const [origin] = useAction(getOrigin); const [editorView, setEditorView] = useState(); const scheduledTimeouts = useRef([]); const [text, setText] = useState(""); @@ -53,9 +56,9 @@ export default function Editor({ // WebSocket connection useEffect(() => { - if (!run) return; + if (!run || !origin?.host) return; - const ws = new WebSocket(`ws://${window.location.host}/api/view-run`); + const ws = new WebSocket(`ws://${origin.host}/api/view-run`); ws.onopen = () => { ws.send(run.id); @@ -97,7 +100,7 @@ export default function Editor({ }; return () => ws.close(); - }, [run]); + }, [run, ]); return ( { - signInWithGitHub(window.location.origin); + signInWithGitHub(); }; const continueWithGoogle = () => {}; diff --git a/src/lib/supabase/server.ts b/src/lib/supabase/server.ts index c9431c0..30a93dc 100644 --- a/src/lib/supabase/server.ts +++ b/src/lib/supabase/server.ts @@ -1,3 +1,5 @@ +"use server"; + import "server-only"; import { createServerClient, type CookieOptions } from "@supabase/ssr"; diff --git a/src/modules/account/actions.ts b/src/modules/account/actions.ts index a364bee..48469f2 100644 --- a/src/modules/account/actions.ts +++ b/src/modules/account/actions.ts @@ -2,15 +2,20 @@ import * as Core from "./core"; import { createClient } from "@/lib/supabase/server"; +import { headers } from "next/headers"; import { redirect } from "next/navigation"; -export async function signInWithGitHub(origin: string) { +export async function signInWithGitHub() { const supabase = await createClient(); + const h = await headers(); + const host = h.get("host"); + const protocol = h.get("x-forwarded-proto") ?? "https"; + const { data, error } = await supabase.auth.signInWithOAuth({ provider: "github", options: { - redirectTo: `${origin}/api/auth/callback`, + redirectTo: `${protocol}://${host}/api/auth/callback`, }, }); diff --git a/src/modules/getOrigin.ts b/src/modules/getOrigin.ts new file mode 100644 index 0000000..82edadb --- /dev/null +++ b/src/modules/getOrigin.ts @@ -0,0 +1,11 @@ +"use server"; + +import { headers } from "next/headers"; + +export async function getOrigin(): Promise<{ protocol: string; host: string }> { + const h = await headers(); + const host = h.get("host") || "localhost:3000"; + const protocol = h.get("x-forwarded-proto") ?? "https"; + + return { protocol, host }; +}