Fix several redirects in frontend and admin

refs #527
This commit is contained in:
Micheil Smith 2013-11-27 02:31:27 +00:00 committed by Fabian Becker
parent dcd3b192c1
commit 3167a9b52c
2 changed files with 41 additions and 28 deletions

View File

@ -73,7 +73,8 @@ adminControllers = {
});
},
'auth': function (req, res) {
var currentTime = process.hrtime()[0],
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
currentTime = process.hrtime()[0],
denied = '';
loginSecurity = _.filter(loginSecurity, function (ipTime) {
return (ipTime.time + 2 > currentTime);
@ -88,8 +89,12 @@ adminControllers = {
req.session.regenerate(function (err) {
if (!err) {
req.session.user = user.id;
res.json(200, {redirect: req.body.redirect ? '/ghost/'
+ decodeURIComponent(req.body.redirect) : '/ghost/'});
var redirect = root + '/ghost/';
if (req.body.redirect) {
redirect += decodeURIComponent(req.body.redirect);
}
res.json(200, {redirect: redirect});
}
});
}, function (error) {
@ -120,7 +125,8 @@ adminControllers = {
});
},
'doRegister': function (req, res) {
var name = req.body.name,
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
name = req.body.name,
email = req.body.email,
password = req.body.password;
@ -135,7 +141,7 @@ adminControllers = {
if (req.session.user === undefined) {
req.session.user = user.id;
}
res.json(200, {redirect: '/ghost/'});
res.json(200, {redirect: root + '/ghost/'});
}
});
});
@ -152,7 +158,8 @@ adminControllers = {
});
},
'generateResetToken': function (req, res) {
var email = req.body.email;
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
email = req.body.email;
api.users.generateResetToken(email).then(function (token) {
var siteLink = '<a href="' + config().url + '">' + config().url + '</a>',
@ -177,7 +184,7 @@ adminControllers = {
};
return api.notifications.add(notification).then(function () {
res.json(200, {redirect: '/ghost/signin/'});
res.json(200, {redirect: root + '/ghost/signin/'});
});
}, function failure(error) {
@ -191,8 +198,9 @@ adminControllers = {
});
},
'reset': function (req, res) {
// Validate the request token
var token = req.params.token;
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
// Validate the request token
token = req.params.token;
api.users.validateToken(token).then(function () {
// Render the reset form
@ -213,12 +221,13 @@ adminControllers = {
errors.logError(err, 'admin.js', "Please check the provided token for validity and expiration.");
return api.notifications.add(notification).then(function () {
res.redirect('/ghost/forgotten');
res.redirect(root + '/ghost/forgotten');
});
});
},
'resetPassword': function (req, res) {
var token = req.params.token,
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
token = req.params.token,
newPassword = req.param('newpassword'),
ne2Password = req.param('ne2password');
@ -231,7 +240,7 @@ adminControllers = {
};
return api.notifications.add(notification).then(function () {
res.json(200, {redirect: '/ghost/signin/'});
res.json(200, {redirect: root + '/ghost/signin/'});
});
}).otherwise(function (err) {
// TODO: Better error message if we can tell whether the passwords didn't match or something
@ -240,15 +249,17 @@ adminControllers = {
},
'logout': function (req, res) {
req.session.destroy();
var notification = {
type: 'success',
message: 'You were successfully signed out',
status: 'passive',
id: 'successlogout'
};
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
notification = {
type: 'success',
message: 'You were successfully signed out',
status: 'passive',
id: 'successlogout'
};
return api.notifications.add(notification).then(function () {
res.redirect('/ghost/signin/');
res.redirect(root + '/ghost/signin/');
});
},
'index': function (req, res) {

View File

@ -19,21 +19,22 @@ var Ghost = require('../../ghost'),
frontendControllers = {
'homepage': function (req, res, next) {
// Parse the page number
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
var root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
// Parse the page number
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
postsPerPage = parseInt(ghost.settings('postsPerPage'), 10),
options = {};
// No negative pages
if (isNaN(pageParam) || pageParam < 1) {
//redirect to 404 page?
return res.redirect('/');
return res.redirect(root + '/');
}
options.page = pageParam;
// Redirect '/page/1/' to '/' for all teh good SEO
if (pageParam === 1 && req.route.path === '/page/:page/') {
return res.redirect('/');
return res.redirect(root + '/');
}
// No negative posts per page, must be number
@ -52,7 +53,7 @@ frontendControllers = {
// If page is greater than number of pages we have, redirect to last page
if (pageParam > maxPage) {
return res.redirect(maxPage === 1 ? '/' : ('/page/' + maxPage + '/'));
return res.redirect(maxPage === 1 ? root + '/' : (root + '/page/' + maxPage + '/'));
}
// Render the page of posts
@ -89,6 +90,7 @@ frontendControllers = {
'rss': function (req, res, next) {
// Initialize RSS
var siteUrl = config().url,
root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path,
pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
feed;
//needs refact for multi user to not use first user as default
@ -105,11 +107,11 @@ frontendControllers = {
// No negative pages
if (isNaN(pageParam) || pageParam < 1) {
return res.redirect('/rss/');
return res.redirect(root + '/rss/');
}
if (pageParam === 1 && req.route.path === '/rss/:page/') {
return res.redirect('/rss/');
if (pageParam === 1 && req.route.path === root + '/rss/:page/') {
return res.redirect(root + '/rss/');
}
api.posts.browse({page: pageParam}).then(function (page) {
@ -123,7 +125,7 @@ frontendControllers = {
// If page is greater than number of pages we have, redirect to last page
if (pageParam > maxPage) {
return res.redirect('/rss/' + maxPage + '/');
return res.redirect(root + '/rss/' + maxPage + '/');
}
ghost.doFilter('prePostsRender', page.posts).then(function (posts) {