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 pug = require('pug');
const config = require('settings/config'); 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) { module.exports.login = function(req, res, render) {
render('login.pug'); render('login.pug');
} }
@ -26,8 +39,12 @@ module.exports.loginTarget = function(req, res, render) {
} else { } else {
if (testPassword(req.body.password, user.password)) { if (testPassword(req.body.password, user.password)) {
req.session.user = user; req.session.user = user;
let redirection = req.session.redirectAfterLog || 'index';
req.session.redirectAfterLog = undefined;
req.session.save(); req.session.save();
res.redirect(getUrl('index'));
res.redirect(getUrl(redirection));
} else { } else {
res.redirect(getUrl('login')); res.redirect(getUrl('login'));
} }

View File

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

View File

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