Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Make authorization clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Konev committed Jun 16, 2015
1 parent e93c684 commit 888cbf2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 47 deletions.
43 changes: 18 additions & 25 deletions server/configs/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
7 changes: 4 additions & 3 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -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('/');
}
};
4 changes: 1 addition & 3 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions server/middleware/express-checkAuth.js
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 11 additions & 12 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion static/desktop.blocks/b-menu/b-menu.priv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 888cbf2

Please sign in to comment.