origin and ws abstractions
This commit is contained in:
@@ -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`);
|
||||
}
|
||||
|
||||
@@ -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 (
|
||||
<CodeMirror
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
"use server";
|
||||
|
||||
import { headers } from "next/headers";
|
||||
import * as Core from "./server";
|
||||
|
||||
export async function getOrigin(): Promise<{ protocol: string; host: string }> {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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}`,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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`,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user