Skip to content

Commit

Permalink
Finished login-check
Browse files Browse the repository at this point in the history
  • Loading branch information
jpuka01 committed Nov 1, 2024
1 parent da71ab8 commit ba0811b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.' });
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,21 +22,21 @@ 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);
alert("Login failed: " + errorMessage);
}
} catch (error) {
console.error('Error during login: ', error);
alert("An error occurred during login");
alert("An error occurred during login.");
}
};

Expand Down

0 comments on commit ba0811b

Please sign in to comment.