Auth handshake

This commit is contained in:
2026-02-16 00:25:29 +01:00
parent 6370219165
commit b6e3dd2349
2 changed files with 197 additions and 185 deletions
+192 -178
View File
@@ -1,190 +1,204 @@
import { WebSocketServer, WebSocket } from "ws"; import { WebSocketServer, WebSocket } from "ws";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import { createClient } from "@/lib/supabase/server"; import { createClientCookie } from "@/lib/supabase/server";
import { getSession } from "@/modules/account/core"; import { getSession } from "@/modules/account/core";
import * as Core from "@/modules/live-run/core"; import * as Core from "@/modules/live-run/core";
import { ClientMessage } from "./types"; import { ClientMessage } from "./types";
import { NextRequest } from "next/server";
export function GET(req: NextRequest) {
const headers = new Headers();
headers.set("Connection", "Upgrade");
headers.set("Upgrade", "websocket");
return new Response("Upgrade Required", { status: 426, headers });
}
export function UPGRADE(client: WebSocket, server: WebSocketServer) { export function UPGRADE(client: WebSocket, server: WebSocketServer) {
client.on("message", async (raw) => { client.once("message", async (raw) => {
try { const { cookies }: { cookies: Record<string, string> } = JSON.parse(
const msg: ClientMessage = JSON.parse(raw.toString()); raw.toString(),
);
const supabase = await createClient(); if (!cookies) {
const session = await getSession(supabase); client.send(JSON.stringify({ ok: false, error: "Missing cookies" }));
client.close();
if (!session) { return;
client.send(JSON.stringify({ ok: false, error: "Invalid session" }));
return;
}
switch (msg.type) {
/* ---------------- CREATE ---------------- */
case "create": {
const { problem, category } = msg;
if (!problem || !category) {
client.send(
JSON.stringify({
ok: false,
error: "problem and category required",
}),
);
return;
}
const runId = randomUUID();
await Core.addLiveRun({
id: runId,
problem,
category,
username: session[1].username,
views: 0,
start: Date.now(),
runsCount: 0,
});
client.send(
JSON.stringify({
ok: true,
type: "create",
data: { runId },
}),
);
break;
}
/* ---------------- DELETE ---------------- */
case "move": {
const { runId, moves, file, language } = msg;
if (
!runId ||
!moves ||
file === undefined ||
language === undefined
) {
client.send(
JSON.stringify({
ok: false,
error: "body requires runId, moves, file and language",
}),
);
return;
}
const supabase = await createClient();
const session = await getSession(supabase);
if (!session) {
client.send(
JSON.stringify({ ok: false, error: "Invalid session" }),
);
return;
}
const liveRun = await Core.getLiveRun(runId);
if (!liveRun) {
client.send(
JSON.stringify({
ok: false,
error: "Run is not live or does not exist",
}),
);
return;
}
const run = await Core.addLiveRunEvent(
liveRun.id,
file,
language,
moves,
);
client.send(
JSON.stringify({
ok: true,
type: "move",
data: run,
}),
);
break;
}
/* ---------------- SUBMIT ---------------- */
case "submit": {
const { runId } = msg;
if (!runId) {
client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
}
const liveRun = await Core.getLiveRun(runId);
if (!liveRun) {
client.send(
JSON.stringify({
ok: false,
error: "Run is not live or does not exist",
}),
);
return;
}
const run = await Core.submitRun(supabase, liveRun, session[0].id);
client.send(
JSON.stringify({
ok: true,
type: "submit",
data: run,
}),
);
break;
}
/* ---------------- DELETE ---------------- */
case "delete": {
const { runId } = msg;
if (!runId) {
client.send(JSON.stringify({ ok: false, error: "runId required" }));
return;
}
const run = await Core.removeLiveRun(runId);
client.send(
JSON.stringify({
ok: true,
type: "delete",
data: run,
}),
);
break;
}
default:
client.send(
JSON.stringify({
ok: false,
error: "Unknown message type",
}),
);
}
} catch (err) {
console.error(err);
client.send(
JSON.stringify({
ok: false,
error: "Malformed message",
}),
);
} }
const supabase = await createClientCookie(new Map(Object.entries(cookies)));
const session = await getSession(supabase);
if (!session) {
client.send(JSON.stringify({ ok: false, error: "Invalid session" }));
return;
}
client.on("message", async (raw) => {
try {
const msg: ClientMessage = JSON.parse(raw.toString());
switch (msg.type) {
/* ---------------- CREATE ---------------- */
case "create": {
const { problem, category } = msg;
if (!problem || !category) {
client.send(
JSON.stringify({
ok: false,
error: "problem and category required",
}),
);
return;
}
const runId = randomUUID();
await Core.addLiveRun({
id: runId,
problem,
category,
username: session[1].username,
views: 0,
start: Date.now(),
runsCount: 0,
});
client.send(
JSON.stringify({
ok: true,
type: "create",
data: { runId },
}),
);
break;
}
/* ---------------- DELETE ---------------- */
case "move": {
const { runId, moves, file, language } = msg;
if (
!runId ||
!moves ||
file === undefined ||
language === undefined
) {
client.send(
JSON.stringify({
ok: false,
error: "body requires runId, moves, file and language",
}),
);
return;
}
const liveRun = await Core.getLiveRun(runId);
if (!liveRun) {
client.send(
JSON.stringify({
ok: false,
error: "Run is not live or does not exist",
}),
);
return;
}
const run = await Core.addLiveRunEvent(
liveRun.id,
file,
language,
moves,
);
client.send(
JSON.stringify({
ok: true,
type: "move",
data: run,
}),
);
break;
}
/* ---------------- SUBMIT ---------------- */
case "submit": {
const { runId } = msg;
if (!runId) {
client.send(
JSON.stringify({ ok: false, error: "runId required" }),
);
return;
}
const liveRun = await Core.getLiveRun(runId);
if (!liveRun) {
client.send(
JSON.stringify({
ok: false,
error: "Run is not live or does not exist",
}),
);
return;
}
const run = await Core.submitRun(supabase, liveRun, session[0].id);
client.send(
JSON.stringify({
ok: true,
type: "submit",
data: run,
}),
);
break;
}
/* ---------------- DELETE ---------------- */
case "delete": {
const { runId } = msg;
if (!runId) {
client.send(
JSON.stringify({ ok: false, error: "runId required" }),
);
return;
}
const run = await Core.removeLiveRun(runId);
client.send(
JSON.stringify({
ok: true,
type: "delete",
data: run,
}),
);
break;
}
default:
client.send(
JSON.stringify({
ok: false,
error: "Unknown message type",
}),
);
}
} catch (err) {
console.error(err);
client.send(
JSON.stringify({
ok: false,
error: "Malformed message",
}),
);
}
});
}); });
client.on("close", () => { client.on("close", () => {
+5 -7
View File
@@ -27,22 +27,20 @@ export async function createClient() {
); );
} }
export async function createClientApi() { export async function createClientCookie(cookies: Map<string, string>) {
const cookieStore = await cookies();
return createServerClient( return createServerClient(
process.env.SUPABASE_URL!, process.env.SUPABASE_URL!,
process.env.SUPABASE_SECRET_KEY!, process.env.SUPABASE_SECRET_KEY!,
{ {
cookies: { cookies: {
get(name: string) { get(name: string) {
return cookieStore.get(name)?.value; return cookies.get(name) || null;
}, },
set(name: string, value: string, options: CookieOptions) { set(name: string, value: string) {
cookieStore.set(name, value, options); cookies.set(name, value);
}, },
remove(name: string, options: CookieOptions) { remove(name: string, options: CookieOptions) {
cookieStore.set(name, "", options); cookies.delete(name);
}, },
}, },
}, },