Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit dea044c

Browse files
committed
further refactoring of the route middlewares so they can be easily shared amongst app routes
1 parent 0afb2e6 commit dea044c

File tree

6 files changed

+47
-50
lines changed

6 files changed

+47
-50
lines changed

app/routes/articles.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
'use strict';
22

3-
module.exports = function(app, passport, auth) {
4-
5-
// Article Routes
6-
var articles = require('../controllers/articles');
3+
// Articles routes use articles controller
4+
var articles = require('../controllers/articles');
5+
var authorization = require('./middlewares/authorization');
6+
7+
// Article authorization helpers
8+
var hasAuthorization = function(req, res, next) {
9+
if (req.article.user.id != req.user.id) {
10+
return res.send(401, 'User is not authorized');
11+
}
12+
next();
13+
}
14+
15+
module.exports = function(app, passport) {
16+
717
app.get('/articles', articles.all);
8-
app.post('/articles', auth.requiresLogin, articles.create);
18+
app.post('/articles', authorization.requiresLogin, articles.create);
919
app.get('/articles/:articleId', articles.show);
10-
app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update);
11-
app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy);
20+
app.put('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.update);
21+
app.del('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.destroy);
1222

1323
// Finish with setting up the articleId param
1424
app.param('articleId', articles.article);

app/routes/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
module.exports = function(app, passport, auth) {
3+
module.exports = function(app, passport) {
44

55
// Home route
66
var index = require('../controllers/index');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
/**
4+
* Generic require login routing middleware
5+
*/
6+
exports.requiresLogin = function(req, res, next) {
7+
if (!req.isAuthenticated()) {
8+
return res.send(401, 'User is not authorized');
9+
}
10+
next();
11+
};

app/routes/users.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
'use strict';
22

3-
module.exports = function(app, passport, auth) {
4-
5-
// User Routes
6-
var users = require('../controllers/users');
3+
// User routes use users controller
4+
var users = require('../controllers/users');
5+
6+
// User authorization helpers
7+
var hasAuthorization = function(req, res, next) {
8+
if (req.profile.id != req.user.id) {
9+
return res.send(401, 'User is not authorized');
10+
}
11+
next();
12+
}
13+
14+
module.exports = function(app, passport) {
15+
716
app.get('/signin', users.signin);
817
app.get('/signup', users.signup);
918
app.get('/signout', users.signout);

config/middlewares/authorization.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
1919

2020
// Initializing system variables
2121
var config = require('./config/config'),
22-
auth = require('./config/middlewares/authorization'),
2322
mongoose = require('mongoose');
2423

2524
// Bootstrap db connection
@@ -58,9 +57,12 @@ var walk = function(path) {
5857
var stat = fs.statSync(newPath);
5958
if (stat.isFile()) {
6059
if (/(.*)\.(js$|coffee$)/.test(file)) {
61-
require(newPath)(app, passport, auth);
60+
require(newPath)(app, passport);
6261
}
63-
} else if (stat.isDirectory()) {
62+
// We skip the app/routes/middlewares directory as it is meant to be
63+
// used and shared by routes as further middlewares and is not a
64+
// route by itself
65+
} else if (stat.isDirectory() && file !== 'middlewares') {
6466
walk(newPath);
6567
}
6668
});

0 commit comments

Comments
 (0)