freeCodeCamp/server/middlewares/add-return-to.js

44 lines
863 B
JavaScript
Raw Normal View History

2015-08-18 22:22:28 +00:00
const pathsOfNoReturn = [
'link',
'auth',
'login',
'logout',
'signin',
'signup',
'fonts',
'favicon',
'js',
'css'
];
2015-09-23 21:54:08 +00:00
const pathsWhiteList = [
'news',
'challenges',
'map',
'news',
'commit'
2015-09-23 21:54:08 +00:00
];
2015-08-18 22:22:28 +00:00
const pathsOfNoReturnRegex = new RegExp(pathsOfNoReturn.join('|'), 'i');
2015-09-23 21:54:08 +00:00
const whiteListRegex = new RegExp(pathsWhiteList.join('|'), 'i');
2015-08-18 22:22:28 +00:00
export default function addReturnToUrl() {
return function(req, res, next) {
// Remember original destination before login.
var path = req.path.split('/')[1];
2015-08-18 22:22:28 +00:00
2015-09-23 21:54:08 +00:00
if (
req.method !== 'GET' ||
pathsOfNoReturnRegex.test(path) ||
!whiteListRegex.test(path) ||
2015-09-23 22:05:27 +00:00
(/news/i).test(path) && (/hot/i).test(req.path)
2015-09-23 21:54:08 +00:00
) {
return next();
}
2016-03-03 04:54:14 +00:00
req.session.returnTo = req.originalUrl === '/map-aside' ?
'/map' :
req.originalUrl;
return next();
};
}