Enabled authtentication by email address

This commit is contained in:
Thomas Forgione 2017-10-09 09:15:07 +02:00
parent f7ff1bbb37
commit 68cc758ab1
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
2 changed files with 27 additions and 15 deletions

View File

@ -9,7 +9,7 @@ block content
| Username or password incorrect. Please try again. | Username or password incorrect. Please try again.
form(method="POST", action=getUrl("loginTarget")) form(method="POST", action=getUrl("loginTarget"))
.form-group .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 .form-group
input.form-control(type="password", name="password", placeholder="Password") input.form-control(type="password", name="password", placeholder="Password")
.form-group .form-group

View File

@ -48,24 +48,36 @@ module.exports.logout = function(req, res, render) {
res.redirect(getUrl("index")); 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) { module.exports.loginTarget = function(req, res, render) {
User.getByUsername(req.body.username, (err, user) => { User.getByUsername(req.body.username, (err, user) => {
if (user === undefined || !user.active) { 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 { } else {
if (testPassword(req.body.password, user.password)) { return logUser(user, req.body.password, req, res, render);
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'));
}
} }
}); });
} }