From ba0811b9e44eb5fcd0c07fe244f751930ddb4dc3 Mon Sep 17 00:00:00 2001 From: John Puka Date: Fri, 1 Nov 2024 18:19:59 -0400 Subject: [PATCH] Finished login-check --- backend/server.js | 17 +++++++++++------ src/pages/Login.jsx | 8 ++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/backend/server.js b/backend/server.js index e6fe035..a781b75 100644 --- a/backend/server.js +++ b/backend/server.js @@ -110,24 +110,29 @@ app.post('/api/users', async (req, res) => { // TODO (Donatello & John): Create an endpoint to receive login data and check if the user exists in the database app.post('/login', async (req, res) => { - const { username } = req.body; + const { username, password } = req.body; - console.log('Received login request:', username ); + // DEBUG: console.log('Received login request:', username ); try { const user = await User.findOne({ username }); console.log('Database query result:', user); if (user) { + if (user.password === password) { console.log('Login successful for user:', username); - res.status(200).send('Login successful'); + res.status(200).send('Login successful!'); + } else { + console.log('Login failed: Incorrect password.'); + res.status(401).send('Invalid password.'); + } } else { console.log('Login failed: User not found'); - res.status(401).send('Invalid username'); + res.status(401).send('Invalid username.'); } } catch (error) { - console.error('Error during login', error); - res.status(500).send({ message: 'Server Error' }); + console.error('Error during login.', error); + res.status(500).send({ message: 'Server Error.' }); } }); diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index ced7a8c..d075ef5 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -13,7 +13,7 @@ export default function Login() { const handleSubmit = async (e) => { e.preventDefault(); - const { username } = formData; + const { username, password } = formData; // alert(`Form submitted with\nusername: ${username}\nand password: ${password}`) try { @@ -22,13 +22,13 @@ export default function Login() { headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ username }), + body: JSON.stringify({ username, password }), }); if (response.ok) { const message = await response.text(); console.log(message); - alert("Login successful"); + alert("Login successful!"); } else { const errorMessage = await response.text(); console.error(errorMessage); @@ -36,7 +36,7 @@ export default function Login() { } } catch (error) { console.error('Error during login: ', error); - alert("An error occurred during login"); + alert("An error occurred during login."); } };