Skip to content
Snippets Groups Projects

Adding toDo app functionability

Merged Jesus Galaz Reyes requested to merge 2-build-javascript-core-functionality into main
All threads resolved!
1 file
+ 0
4
Compare changes
  • Side-by-side
  • Inline
+ 0
4
@@ -31,25 +31,21 @@ router.post('/register', async (req, res) => {
@@ -31,25 +31,21 @@ router.post('/register', async (req, res) => {
// LogIn User
// LogIn User
router.post('/login', async (req, res) => {
router.post('/login', async (req, res) => {
const { username, password } = req.body;
const { username, password } = req.body;
console.log('Attempting to log in:', username);
try {
try {
const user = await User.findOne({ username });
const user = await User.findOne({ username });
console.log('User found:', user);
if (!user) {
if (!user) {
return res.status(400).json({ error: 'Invalid username or password' });
return res.status(400).json({ error: 'Invalid username or password' });
}
}
const isMatch = await bcrypt.compare(password, user.password);
const isMatch = await bcrypt.compare(password, user.password);
console.log('Password match:', isMatch);
if (!isMatch) {
if (!isMatch) {
return res.status(400).json({ error: 'Invalid username or password' });
return res.status(400).json({ error: 'Invalid username or password' });
}
}
req.session.user = { id: user._id, username: user.username };
req.session.user = { id: user._id, username: user.username };
console.log('User logged in:', req.session.user);
res.json({ success: true, message: 'Login successful' });
res.json({ success: true, message: 'Login successful' });
} catch (error) {
} catch (error) {
Loading