diff --git a/backend/src/server.ts b/backend/src/server.ts index a76848d..4cabd64 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -26,10 +26,10 @@ io.on("connection", (socket) => { console.log(`🟢 User connected: ${socket.id}`); // ---------------- Join Room ---------------- - socket.on("join-room", async (roomId: string, userName: string) => { - try { - socket.join(roomId); - console.log(`👥 ${userName} joined room ${roomId}`); + socket.on("join-room", async ({ roomId, userName }: { roomId: string; userName: string }) => { + try { + socket.join(roomId); + console.log(`👥 ${userName} joined room ${roomId}`); // Notify others io.to(roomId).emit("user-joined", { userName, roomId }); diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 87ea27c..c06542a 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -15,6 +15,7 @@ import InRoom from "./pages/InRoom.js"; import CreateRoomLobby from "./pages/CreateRoomLobby.js"; import CreateRoom from "./pages/CreateRoom.js"; import ActiveSessions from "./pages/ActiveSessions.js"; +import InRoomWrapper from "./pages/InRoomWrapper.js"; const queryClient = new QueryClient(); const App = () => ( @@ -32,7 +33,7 @@ const App = () => ( } /> } /> {/* ✅ */} } /> {/* ✅ */} - } /> + } /> } /> } /> diff --git a/frontend/src/pages/CreateRoomLobby.tsx b/frontend/src/pages/CreateRoomLobby.tsx index b61995d..80315a2 100644 --- a/frontend/src/pages/CreateRoomLobby.tsx +++ b/frontend/src/pages/CreateRoomLobby.tsx @@ -29,7 +29,7 @@ export default function CreateRoomLobby() { } }; fetchRoom(); - }, [roomId]); + }, [roomId,roomName]); // Copy join link to clipboard const handleCopyLink = () => { @@ -73,7 +73,7 @@ export default function CreateRoomLobby() {

+
+ + {msg.text} +
- + + ))} +
+
)} - + - {/* Controls */} -
- + + + )} + - + {/* Footer Controls */} +
+ - + - -
- - ); + + + +
+ + + ); }; export default InRoom; diff --git a/frontend/src/pages/InRoomWrapper.tsx b/frontend/src/pages/InRoomWrapper.tsx new file mode 100644 index 0000000..3332aac --- /dev/null +++ b/frontend/src/pages/InRoomWrapper.tsx @@ -0,0 +1,50 @@ +import { useParams, useLocation } from "react-router-dom"; +import InRoom from "./InRoom.js"; +import React, { useEffect, useState } from "react"; +import axios from "axios"; + +const InRoomWrapper: React.FC = () => { + const { roomName } = useParams(); + const location = useLocation(); + + const [userName, setUserName] = useState(null); + + useEffect(() => { + let mounted = true; + + const fetchUser = async () => { + try { + const token = localStorage.getItem("token"); + const res = await axios.get("http://localhost:3000/api/auth/me", { + headers: token ? { Authorization: `Bearer ${token}` } : undefined, + withCredentials: true, + }); + const name = res.data?.user?.name; + if (mounted && name) { + setUserName(name); + return; + } + } catch (err) { + // ignore and fallback below + } + + const navName = (location.state as { userName?: string })?.userName; + const stored = localStorage.getItem("userName"); + if (mounted) setUserName(navName || stored || "Anonymous"); + }; + + fetchUser(); + + return () => { + mounted = false; + }; + }, [location]); + + if (userName === null) { + return
Loading...
; + } + + return ; +}; + +export default InRoomWrapper; \ No newline at end of file