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) {
|
export async function GET(req: NextRequest) {
|
||||||
const code = req.nextUrl.searchParams.get("code");
|
const code = req.nextUrl.searchParams.get("code");
|
||||||
const { protocol, host } = await getOrigin();
|
const { httpOrigin } = await getOrigin();
|
||||||
|
|
||||||
if (!code) {
|
if (!code) {
|
||||||
return NextResponse.json({ error: "code param required" }, { status: 400 });
|
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.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
|
// WebSocket connection
|
||||||
useEffect(() => {
|
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.onopen = () => {
|
||||||
ws.send(run.id);
|
ws.send(run.id);
|
||||||
@@ -100,7 +100,7 @@ export default function Editor({
|
|||||||
};
|
};
|
||||||
|
|
||||||
return () => ws.close();
|
return () => ws.close();
|
||||||
}, [run, ]);
|
}, [run]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CodeMirror
|
<CodeMirror
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { headers } from "next/headers";
|
import * as Core from "./server";
|
||||||
|
|
||||||
export async function getOrigin(): Promise<{ protocol: string; host: string }> {
|
export async function getOrigin() {
|
||||||
const h = await headers();
|
return Core.getOrigin();
|
||||||
const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000";
|
|
||||||
const protocol = h.get("x-forwarded-proto") || h.get(":scheme") || "https";
|
|
||||||
|
|
||||||
return { protocol, host };
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
import { headers } from "next/headers";
|
|
||||||
import "server-only";
|
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 h = await headers();
|
||||||
const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000";
|
const host = process.env.ORIGIN || h.get(":authority") || "localhost:3000";
|
||||||
const protocol =
|
const protocol =
|
||||||
@@ -10,5 +17,13 @@ export async function getOrigin(): Promise<{ protocol: string; host: string }> {
|
|||||||
h.get(":scheme") ||
|
h.get(":scheme") ||
|
||||||
"https";
|
"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() {
|
export async function signInWithGitHub() {
|
||||||
const supabase = await createClient();
|
const supabase = await createClient();
|
||||||
|
|
||||||
const { host, protocol } = await getOrigin();
|
const { httpOrigin } = await getOrigin();
|
||||||
|
|
||||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||||
provider: "github",
|
provider: "github",
|
||||||
options: {
|
options: {
|
||||||
redirectTo: `${protocol}://${host}/api/auth/callback`,
|
redirectTo: `${httpOrigin}/api/auth/callback`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user