diff --git a/server/configs/default.js b/server/configs/default.js index 4486d31..2b2851e 100644 --- a/server/configs/default.js +++ b/server/configs/default.js @@ -9,33 +9,26 @@ module.exports = { languageId: 'ru' }, - defaults: function(req ){ + defaults: function(req){ + req.user = req.user ? req.user : {}; + + var defaultMenuItems = [ + { title: 'Главная', url: '/' }, + { title: 'Все пользователи', url: '/users' }, + { title: req.user ? 'Мои товары' : 'Все товары', url: '/goods'} + ], + authorizedMenuItems = [ + { title: req.user.username, url: '/account' }, + { title: 'Выход', url: '/logout' } + ], + notAuthorizedMenuItems = [ + { title: 'Авторизоваться', url: '/login' } + ]; + return { user: req.user, - language: 'ru', - menu: [ - { - title: 'Главная', - url: '/' - }, - { - title: 'Все пользователи', - url: '/users' - }, - { - title: req.user ? 'Мои товары' : 'Все товары', - url: '/goods' - }, - !req.user ? - { - title: 'Авторизоваться', - url: '/auth' - } : - { - title: req.user.username, - url: '/account' - } - ], + language: 'ru', + menu: defaultMenuItems.concat(req.user.username ? authorizedMenuItems : notAuthorizedMenuItems), route: req.route } } diff --git a/server/controllers/auth.js b/server/controllers/auth.js index d5c4399..3c67b29 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -1,9 +1,10 @@ module.exports = { - passportAuth: function(req, res) { - + passportLoginCb: function(req, res) { + res.redirect('/account'); }, - passportCallback: function(req, res) { + passportLogout: function(req, res) { + req.logout(); res.redirect('/'); } }; diff --git a/server/controllers/index.js b/server/controllers/index.js index d89b560..840c499 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,12 +1,10 @@ -var ensureAuthanticated = require('../middleware/express-checkAuth'), - configs = require('../configs/default'); +var configs = require('../configs/default'); module.exports = { index: function(req, res, next) { var data = configs.defaults(req); data.isAccount = req.route.path === '/account'; - data.isAccount && ensureAuthanticated(req, res, next); res.render('index', data, function(err, html) { if (err) { diff --git a/server/middleware/express-checkAuth.js b/server/middleware/express-checkAuth.js index 3f12bc4..7343657 100644 --- a/server/middleware/express-checkAuth.js +++ b/server/middleware/express-checkAuth.js @@ -1,9 +1,9 @@ -function ensureAuthenticated(req, res) { +function ensureAuthenticated(req, res, next) { if (req.isAuthenticated()) { - return; + return next(); } - res.redirect('/login') + return res.redirect('/login') } module.exports = ensureAuthenticated; \ No newline at end of file diff --git a/server/router.js b/server/router.js index 402cddc..7c8a328 100644 --- a/server/router.js +++ b/server/router.js @@ -2,22 +2,21 @@ var index = require('./controllers/index'), db = require('./controllers/db'), auth = require('./controllers/auth'), - passport = require('./lib/passport'); + passport = require('./lib/passport'), + + ensureAuthanticated = require('./middleware/express-checkAuth'); module.exports = function (app) { - app.get('/', index.index); - app.get('/account', index.index); - app.get('/goods', index.index); - app.get('/users', index.index); + app.get('/', index.index); + app.get('/goods', index.index); + app.get('/account', ensureAuthanticated, index.index); + app.get('/users', ensureAuthanticated, index.index); // авторизация - app.get('/login', function(req, res) { res.redirect('/auth') }); - app.get('/auth', - passport.authenticate('yandex'), - auth.passportAuth); - app.get('/auth/yandex/callback', - passport.authenticate('yandex', { failureRedirect: '/auth' }), - auth.passportCallback); + app.get('/login', passport.authenticate('yandex')); + app.get('/login/callback', passport.authenticate('yandex', { failureRedirect: '/login' }), auth.passportLoginCb); + app.get('/logout', auth.passportLogout); + //some mongodb routes //app.get('/get/', db.getGoods); diff --git a/static/desktop.blocks/b-menu/b-menu.priv.js b/static/desktop.blocks/b-menu/b-menu.priv.js index 405262c..0fc244f 100644 --- a/static/desktop.blocks/b-menu/b-menu.priv.js +++ b/static/desktop.blocks/b-menu/b-menu.priv.js @@ -5,7 +5,7 @@ module.exports = function (blocks) { block: 'b-menu', content: (function(){ return data.menu.map(function (item) { - return { + return item && { elem: 'item', content: { block: 'b-link',