From 2a63a628a4982768a9449ac2c85d42c294a5be62 Mon Sep 17 00:00:00 2001 From: vincentastolfi Date: Thu, 8 Aug 2024 09:19:17 +0200 Subject: [PATCH] :wrench: improve cookies usage and error handling on expired cookies --- index.js | 47 +++++++++++++++++++++++++++------- public/scripts/connection.js | 4 --- public/scripts/gamePageInfo.js | 15 ++++++++--- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 0ffb683..af91a9c 100644 --- a/index.js +++ b/index.js @@ -19,24 +19,27 @@ app.use(cookieParser()); const secretKey = process.env.COOKIE_SECRET_KEY; +// #region routing and cookies + app.get('/', (req, res) => { const token = req.cookies.authToken; if(token) { - res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) + try { + jwt.verify(token, secretKey); + res.status(200) + res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) + } catch (ex) { + res.status(401) + res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) + } } else { res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) } }) app.get('/game', (req, res) => { - const token = req.cookies.authToken; - - if (!token) { - res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) - } else { - res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) - } + res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) }) app.post('/logIn', (req, res) => { @@ -120,6 +123,11 @@ app.get('/user-info', (req, res) => { } }); +// #endregion routing and cookies + + +// #region socket and game + let rooms = []; let players = []; @@ -128,8 +136,9 @@ io.on("connection", (socket) => { socket.on("disconnect", () => { const index = players.findIndex((p) => p.id === socket.id) + // if there is no room this line creates error const roomIndex = rooms.findIndex(room => - room.players.some(player => player.id === socket.id) + room.players.some((player) => player.id === socket.id) ); if (roomIndex !== -1) { @@ -154,6 +163,7 @@ io.on("connection", (socket) => { }) socket.on("first connection", (socketId) => { + // error my occurs if cookie is expired const cookies = socket.request.headers.cookie; const authToken = cookies.split('; ').find(cookie => cookie.startsWith('authToken=')).split('=')[1]; @@ -256,6 +266,22 @@ io.on("connection", (socket) => { }); }); + socket.on("game ended", (roomId) => { + const room = rooms.find((r) => r.id === roomId) + const roomIndex = rooms.findIndex((r) => r.id === roomId) + + room.players.forEach(player => { + player.resetGrid() + io.to(player.id).emit("go to menu") + }); + + room.players = [] + rooms.slice(roomIndex, 1) + delete room + + console.log("rooms list : ", rooms) + }) + socket.on("reset grid", (roomId) => { const player = rooms.find((r) => r.id === roomId).players[0] player.resetGrid(); @@ -288,6 +314,9 @@ const sendMoveToPlayers = (moveData) => { askToPlay(moveData.player) }; +// #endregion socket and game + + http.listen(port, () => { console.log(`Listening on http://localhost:${port}`); }); \ No newline at end of file diff --git a/public/scripts/connection.js b/public/scripts/connection.js index e3a8068..723c2a1 100644 --- a/public/scripts/connection.js +++ b/public/scripts/connection.js @@ -15,8 +15,6 @@ document.getElementById('registerForm').addEventListener('submit', async functio }); const result = await response.json(); - console.log("test") - console.log(result) if (response.ok) { messageDiv.textContent = 'User registered successfully!'; @@ -27,7 +25,6 @@ document.getElementById('registerForm').addEventListener('submit', async functio messageDiv.style.color = 'red'; } } catch (error) { - console.log("testE") messageDiv.textContent = `Error: ${error.message}`; messageDiv.style.color = 'red'; } @@ -60,7 +57,6 @@ document.getElementById('logInForm').addEventListener('submit', async function ( messageDiv.style.color = 'red'; } } catch (error) { - console.log("testEL") messageDiv.textContent = `Error: ${error.message}`; messageDiv.style.color = 'red'; } diff --git a/public/scripts/gamePageInfo.js b/public/scripts/gamePageInfo.js index 65544b8..b684368 100644 --- a/public/scripts/gamePageInfo.js +++ b/public/scripts/gamePageInfo.js @@ -1,3 +1,7 @@ +function deleteCookie(name) { + document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; +} + document.addEventListener('DOMContentLoaded', async () => { const playerInfoDiv = document.getElementById('playerInfo'); @@ -9,11 +13,16 @@ document.addEventListener('DOMContentLoaded', async () => { }, }); - if (response.ok) { + if (response.status === 401) { + deleteCookie('authToken'); + window.location.href = '/'; + } else if (!response.ok) { + deleteCookie('authToken'); + playerInfoDiv.textContent = 'Error: Could not retrieve user information.'; + window.location.href = '/'; + } else { const userInfo = await response.json(); playerInfoDiv.textContent = `Logged in as: ${userInfo.pseudo}`; - } else { - playerInfoDiv.textContent = 'Error: Could not retrieve user information.'; } } catch (error) { playerInfoDiv.textContent = `Error: ${error.message}`;