Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login check #24

Merged
merged 8 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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