diff --git a/src/controllers/index.js b/src/controllers/index.js index e43d08a9c1..3e94107268 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -183,6 +183,11 @@ Controllers.login = function(req, res, next) { }; Controllers.register = function(req, res, next) { + + if (req.user) { + res.redirect('/'); + } + var data = {}, login_strategies = auth.get_login_strategies(), num_strategies = login_strategies.length; @@ -206,7 +211,7 @@ Controllers.register = function(req, res, next) { data.maximumUsernameLength = meta.config.maximumUsernameLength; data.minimumPasswordLength = meta.config.minimumPasswordLength; data.termsOfUse = meta.config.termsOfUse; - + res.render('register', data); }; diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 2992d87fc7..c964533b33 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -22,7 +22,7 @@ var app, middleware.authenticate = function(req, res, next) { if(!req.user) { if (res.locals.isAPI) { - return res.json(403, 'not-allowed'); + return res.json(403, 'not-allowed'); } else { return res.redirect('403'); } @@ -41,6 +41,17 @@ middleware.updateLastOnlineTime = function(req, res, next) { next(); }; +middleware.redirectToAccountIfLoggedIn = function(req, res, next) { + if (req.user) { + user.getUserField(req.user.uid, 'userslug', function (err, userslug) { + res.redirect('/user/' + userslug); + }); + } else { + next(); + } + +} + middleware.prepareAPI = function(req, res, next) { res.locals.isAPI = true; next(); diff --git a/src/routes/index.js b/src/routes/index.js index 72bc3cbfa5..c54053d4f5 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -28,11 +28,11 @@ module.exports = function(app, middleware) { app.get('/', middleware.buildHeader, controllers.home); app.get('/api/home', controllers.home); - app.get('/login', middleware.buildHeader, controllers.login); - app.get('/api/login', controllers.login); + app.get('/login', middleware.redirectToAccountIfLoggedIn, middleware.buildHeader, controllers.login); + app.get('/api/login', middleware.redirectToAccountIfLoggedIn, controllers.login); - app.get('/register', middleware.buildHeader, controllers.register); - app.get('/api/register', controllers.register); + app.get('/register', middleware.redirectToAccountIfLoggedIn, middleware.buildHeader, controllers.register); + app.get('/api/register', middleware.redirectToAccountIfLoggedIn, controllers.register); app.get('/confirm/:code', middleware.buildHeader, controllers.confirmEmail); app.get('/api/confirm/:code', controllers.confirmEmail);