From 99aa32eb8b4ab221e9a06acd3cc6da5e259eba28 Mon Sep 17 00:00:00 2001 From: vincentastolfi Date: Tue, 11 Jun 2024 17:23:23 +0200 Subject: [PATCH] :sparkles: account creation, new page routing. Cleaner code --- index.js | 9 +++++---- public/{index.html => pages/gameView.html} | 0 public/scripts/connection.js | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) rename public/{index.html => pages/gameView.html} (100%) diff --git a/index.js b/index.js index 96e25ff..72165a7 100644 --- a/index.js +++ b/index.js @@ -14,11 +14,12 @@ app.use(express.json()); const { Player } = require(`${__dirname}/businesses/Player.js`); app.get('/', (req, res) => { - res.sendFile(path.join(__dirname, '/public/index.html')) + res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) }) -app.get('/register', (req, res) => { - res.sendFile(path.join(__dirname, '/public/pages/connectionView.html')) +app.get('/game', (req, res) => { + console.log('get game') + res.sendFile(path.join(__dirname, '/public/pages/gameView.html')) }) app.post('/register', (req, res) => { @@ -35,7 +36,7 @@ app.post('/register', (req, res) => { return res.status(500).send('Internal server error.'); } - res.status(201).send('User registered successfully.'); + res.status(201).send({message: 'User registered successfully.', redirectUrl: '/game' }); }) }); diff --git a/public/index.html b/public/pages/gameView.html similarity index 100% rename from public/index.html rename to public/pages/gameView.html diff --git a/public/scripts/connection.js b/public/scripts/connection.js index c972af8..d98e4bf 100644 --- a/public/scripts/connection.js +++ b/public/scripts/connection.js @@ -16,12 +16,14 @@ document.getElementById('registerForm').addEventListener('submit', async functio body: JSON.stringify({ pseudo, password }), }); + const result = await response.json(); + if (response.ok) { messageDiv.textContent = 'User registered successfully!'; messageDiv.style.color = 'green'; + window.location.href = result.redirectUrl; } else { - const errorText = await response.text(); - messageDiv.textContent = `Error: ${errorText}`; + messageDiv.textContent = `Error: ${result.message || 'Unknown error'}`; messageDiv.style.color = 'red'; } } catch (error) {