diff --git a/main.py b/main.py index dcda38d..8f99694 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,13 @@ def settings(): def room(room_code): return render_template('room.html', room_code=room_code) +@app.route('/api/room/', methods=['GET']) +def check_room(code): + room = registry.get_room(code) + if not room: + return jsonify({"error": "Room not found"}), 404 + return jsonify({"code": room.code}) + @socketio.on('message') def handle_message(data): room_code = data['room'] diff --git a/resource/static/js/home.js b/resource/static/js/home.js index 372289c..e6df464 100644 --- a/resource/static/js/home.js +++ b/resource/static/js/home.js @@ -30,4 +30,33 @@ document.getElementById('create-button').addEventListener('click', async () => { } } catch (_) { } +}); + +document.getElementById('join-button').addEventListener('click', async () => { + try { + const username = getUsername(); + const roomCode = document.getElementById('room-code-input').value.trim(); + + if (!roomCode) { + alert("Please enter a room code."); + return; + } + + // Verify the room exists by trying to join it + const res = await fetch(`/api/room/${roomCode}`, { + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }); + + if (res.ok) { + const allUsernames = JSON.parse(localStorage.getItem('usernames') || '{}'); + allUsernames[roomCode] = pendingUsername; + localStorage.setItem('usernames', JSON.stringify(allUsernames)); + + window.location.href = `/room/${roomCode}`; + } else { + alert("Room not found or invalid code."); + } + } catch (_) { + } }); \ No newline at end of file