Added redirectIfNotLogged decorator.

Fixes #5
This commit is contained in:
Thomas Forgione 2017-09-26 11:42:31 +02:00
parent 19a99622a2
commit aa9ce199b0
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
3 changed files with 25 additions and 5 deletions

View File

@ -5,6 +5,19 @@ const mail = require('mail');
const pug = require('pug');
const config = require('settings/config');
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);
};
}
module.exports.login = function(req, res, render) {
render('login.pug');
}
@ -26,8 +39,12 @@ module.exports.loginTarget = function(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('index'));
res.redirect(getUrl(redirection));
} else {
res.redirect(getUrl('login'));
}

View File

@ -1,9 +1,11 @@
const getUrl = require('create-url').getUrl;
const User = require('auth/models');
module.exports.settings = function(req, res, render) {
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
module.exports.settings = redirectIfNotLogged('settings', function(req, res, render) {
render('settings.pug');
}
});
module.exports.settingsTarget = function(req, res, render) {
let projectId = parseInt(req.body.projectId, 10);

View File

@ -1,4 +1,5 @@
const cal = require('calendar');
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
function computeUserTable(user, callback) {
@ -33,9 +34,9 @@ function computeUserTable(user, callback) {
});
}
module.exports.total = function(req, res, render) {
module.exports.total = redirectIfNotLogged('total', function(req, res, render) {
render('total.pug');
}
});
module.exports.totalTable = function(req, res, render) {
computeUserTable(req.session.user, (result) => {