From 936992ce0e4f49602c25ca8b3db8d1b0632087d1 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 19 Oct 2025 15:11:27 +0200 Subject: [PATCH] Tiny auth error tweaks, also ! on greet --- src/app/chat/page.tsx | 2 +- src/lib/auth.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/app/chat/page.tsx b/src/app/chat/page.tsx index 3dd8096..e4bac80 100644 --- a/src/app/chat/page.tsx +++ b/src/app/chat/page.tsx @@ -40,7 +40,7 @@ export default function ChatHub() {
app.setSidebarOpen(true)}>

- Hey {app.profile?.display_name || "there"} + Hey {app.profile?.display_name || "there"}!

Here's what's happening in your network today. diff --git a/src/lib/auth.ts b/src/lib/auth.ts index 5b7bafa..2fcc413 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -2,6 +2,7 @@ import { Message, Server } from "@/types/types"; import axios from "axios"; import { Dispatch, RefObject, SetStateAction } from "react"; import { get } from "./request"; +import { toast } from "sonner"; export default async function auth( id: string, @@ -22,6 +23,8 @@ export default async function auth( (await axios.post("/api/auth", { server_id: id })).data as any ).token; + var name = id; + // Open the WebSocket connection const ws = new WebSocket(`wss://${ip}`); wsRef.current = ws; @@ -34,6 +37,13 @@ export default async function auth( const data = JSON.parse(m.data); console.log("Message received:", data); + if (data.error) { + toast.error(`Error from ${name} (${data.error})`, { + description: data.message, + }); + return; + } + if (data.version) { setServer({ channels: [], ...data }); ws.send( @@ -43,6 +53,7 @@ export default async function auth( last_message: lastMessage || 0, }) ); + name = data.name; return; } @@ -60,10 +71,28 @@ export default async function auth( ws.onerror = (err) => { console.error("WebSocket error:", err); + + let description = "An unknown WebSocket error occurred."; + + if (err instanceof ErrorEvent) { + // Browser WebSocket errors show up here + description = + err.message || err.error?.message || "WebSocket connection failed."; + } else if (err instanceof Event) { + // Generic Event (no info) + description = "WebSocket encountered a network issue."; + } else if (err && typeof err === "object") { + // Custom data if something special got passed + description = JSON.stringify(err, null, 2); + } else { + description = String(err); + } + + toast.error(`WebSocket error (${name})`, { description }); }; ws.onclose = () => { - console.log("WebSocket closed:", ip); + console.log("WebSocket closed: ", ip); }; // Cleanup on unmount or id change