From 68cc758ab130fedf6ab3635c125911f0f442789d Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 9 Oct 2017 09:15:07 +0200 Subject: [PATCH] Enabled authtentication by email address --- controllers/auth/templates/login.pug | 2 +- controllers/auth/views.js | 40 ++++++++++++++++++---------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/controllers/auth/templates/login.pug b/controllers/auth/templates/login.pug index 7dcb628..beb83b5 100644 --- a/controllers/auth/templates/login.pug +++ b/controllers/auth/templates/login.pug @@ -9,7 +9,7 @@ block content | Username or password incorrect. Please try again. form(method="POST", action=getUrl("loginTarget")) .form-group - input.form-control(type="text", name="username", placeholder="Username", autofocus) + input.form-control(type="text", name="username", placeholder="Username or email", autofocus) .form-group input.form-control(type="password", name="password", placeholder="Password") .form-group diff --git a/controllers/auth/views.js b/controllers/auth/views.js index 3e1bc55..d1ef4c2 100644 --- a/controllers/auth/views.js +++ b/controllers/auth/views.js @@ -48,24 +48,36 @@ module.exports.logout = function(req, res, render) { res.redirect(getUrl("index")); } +function logUser(user, password, req, res, render) { + if (testPassword(password, user.password)) { + req.session.user = user; + + let redirection = req.session.redirectAfterLog || 'index'; + req.session.redirectAfterLog = undefined; + req.session.save(); + + res.redirect(getUrl(redirection)); + } else { + req.session.loginFailed = true; + req.session.save(); + res.redirect(getUrl('login')); + } + +} + module.exports.loginTarget = function(req, res, render) { User.getByUsername(req.body.username, (err, user) => { if (user === undefined || !user.active) { - res.redirect(getUrl('login')); + // Try with email address + User.getByEmail(req.body.username, (err, user) => { + if (user === undefined || !user.active) { + res.redirect(getUrl('login')); + } else { + return logUser(user, req.body.password, req, res, render); + } + }); } else { - if (testPassword(req.body.password, user.password)) { - req.session.user = user; - - let redirection = req.session.redirectAfterLog || 'index'; - req.session.redirectAfterLog = undefined; - req.session.save(); - - res.redirect(getUrl(redirection)); - } else { - req.session.loginFailed = true; - req.session.save(); - res.redirect(getUrl('login')); - } + return logUser(user, req.body.password, req, res, render); } }); }