adejs/controllers/auth/views.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-09-23 18:20:56 +02:00
const testPassword = require('model').PasswordField.testSync;
2017-09-23 14:53:56 +02:00
const User = require('./models.js');
2017-09-23 11:39:42 +02:00
const getUrl = require('create-url').getUrl;
2017-09-23 18:20:56 +02:00
const mail = require('mail');
const pug = require('pug');
const config = require('settings/config');
2017-09-23 11:39:42 +02:00
module.exports.redirectIfNotLogged = function(redirectionPage, toDecorate) {
return function(req, res, render) {
if (req.session.user === undefined) {
req.session.redirectAfterLog = redirectionPage;
req.session.save();
res.redirect(getUrl('login'));
return;
}
return toDecorate(...arguments);
};
}
2017-09-23 11:39:42 +02:00
module.exports.login = function(req, res, render) {
render('login.pug');
}
2017-09-23 18:20:56 +02:00
module.exports.signup = function(req, res, render) {
render('signup.pug');
}
2017-09-23 11:39:42 +02:00
module.exports.logout = function(req, res, render) {
2017-09-23 18:20:56 +02:00
req.session.user = undefined;
req.session.save();
2017-09-23 11:39:42 +02:00
res.redirect(getUrl("index"));
}
2017-09-23 18:20:56 +02:00
module.exports.loginTarget = function(req, res, render) {
User.getByUsername(req.body.username, (err, user) => {
if (user === undefined || !user.active) {
res.redirect(getUrl('login'));
} else {
if (testPassword(req.body.password, user.password)) {
req.session.user = user;
let redirection = req.session.redirectAfterLog || 'index';
req.session.redirectAfterLog = undefined;
2017-09-23 18:20:56 +02:00
req.session.save();
res.redirect(getUrl(redirection));
2017-09-23 18:20:56 +02:00
} else {
res.redirect(getUrl('login'));
}
}
});
}
module.exports.signupTarget = function(req, res, render) {
let user = new User();
user.username = req.body.username;
user.email = req.body.email;
user.password = req.body.password;
require('crypto').randomBytes(48, function(err, buffer) {
user.activationKey = buffer.toString('hex');
user.save((err) => {
res.locals.user = user;
let baseUrl =
req.headers.referer.split('/').slice(0, 3).join('/') + '/'
res.locals.activationUrl =
baseUrl + 'activate/' + user.activationKey;
let html = pug.renderFile(
__dirname + '/templates/validation-mail.pug',
res.locals,
);
mail({
from: config.MAIL.FROM,
to: user.username + ' <' + user.email + '>',
subject: 'Welcome on ADEjs!',
text:'',
attachment: [
{data: html, alternative: true}
]
}, (err, result) => {
res.redirect(getUrl('mailWasSent'));
});
});
});
}
module.exports.mailWasSent = function(req, res, render) {
render('mailWasSent.pug');
}
module.exports.activate = function(req, res, render, next) {
User.getByActivationKey(req.params.activationKey, (err, user) => {
if (user === undefined) {
return next();
}
user.active = true;
user.activationKey = undefined;
user.save((err, user) => {
req.session.user = user;
req.session.save();
2017-09-25 21:39:14 +02:00
res.redirect(getUrl('settings'));
2017-09-23 18:20:56 +02:00
});
});
}