Skip to content

Commit

Permalink
Merge pull request #24 from JumboCode/login-check
Browse files Browse the repository at this point in the history
Login check
  • Loading branch information
myix765 authored Nov 2, 2024
2 parents c135884 + ba0811b commit 538fc05
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
26 changes: 26 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ app.post('/api/users', async (req, res) => {
// Login

// 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, password } = req.body;

// 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!');
} 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.');
}
} catch (error) {
console.error('Error during login.', error);
res.status(500).send({ message: 'Server Error.' });
}
});


// Contact
Expand Down
25 changes: 15 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,34 @@ export default function Login() {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
const { username, password } = formData
alert(`Form submitted with\nusername: ${username}\nand password: ${password}`)
}
const { username, password } = formData;
// alert(`Form submitted with\nusername: ${username}\nand password: ${password}`)

try {
const response = await fetch('http://localhost:4000/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});

if (response.ok) {
const message = await response.text();
console.log(message);
alert("Login successful!");
} else {
const errorMessage = await response.text();
console.error(errorMessage);
alert("Login failed: " + errorMessage);
}
} catch (error) {
console.error('Error during login: ', error);
alert("An error occurred during login.");
}
};

return (
<>
Expand Down

0 comments on commit 538fc05

Please sign in to comment.