From 2a442dde0ebae9de9c2832f7774f107050802f43 Mon Sep 17 00:00:00 2001 From: Hajar RAHMOUNI Date: Thu, 5 Oct 2023 23:33:25 +0200 Subject: [PATCH] fix : error code 400 when wrong credantials --- routes/index.js | 5 +++-- test/routes.test.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/routes/index.js b/routes/index.js index 53ef324..1afdd2b 100644 --- a/routes/index.js +++ b/routes/index.js @@ -20,19 +20,20 @@ router.post('/login', function(req, res, next) { } if (!user) { console.log('invalid username or password'); - return res.render('login', { error: ' Invalid username or password' }); + res.status(400).render('login', { error: 'Invalid username or password' }); + return; } req.logIn(user, function(err) { if (err) { return next(err); } - // If authentication is successful, redirect to the dashboard return res.redirect('/dashboard'); }); })(req, res, next); }); + router.get('/dashboard', isAuthenticated, function(req, res, next) { res.render('dashboard', { user: req.user }); }); diff --git a/test/routes.test.js b/test/routes.test.js index 0e076f9..4131f0d 100644 --- a/test/routes.test.js +++ b/test/routes.test.js @@ -38,7 +38,7 @@ describe('Authentication Routes', () => { .post('/login') .send({ username: 'invalidUser', password: 'invalidPassword' }) .end((err, res) => { - expect(res).to.have.status(200); + expect(res).to.have.status(400); expect(res.text).to.include('Invalid username or password'); done(); }); -- GitLab