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

Commit

Permalink
Merge branch 'master' of https://github.com/lirantal/mean into lirant…
Browse files Browse the repository at this point in the history
…al-master
  • Loading branch information
liorkesos committed Jan 21, 2014
2 parents 526fc0a + 8324179 commit d59bf86
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 60 deletions.
26 changes: 26 additions & 0 deletions app/routes/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

// Articles routes use articles controller
var articles = require('../controllers/articles');
var authorization = require('./middlewares/authorization');

// Article authorization helpers
var hasAuthorization = function(req, res, next) {
if (req.article.user.id !== req.user.id) {
return res.send(401, 'User is not authorized');
}
next();
};

module.exports = function(app) {

app.get('/articles', articles.all);
app.post('/articles', authorization.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.update);
app.del('/articles/:articleId', authorization.requiresLogin, hasAuthorization, articles.destroy);

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

};
9 changes: 9 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app) {

// Home route
var index = require('../controllers/index');
app.get('/', index.render);

};
11 changes: 11 additions & 0 deletions app/routes/middlewares/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

/**
* Generic require login routing middleware
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, 'User is not authorized');
}
next();
};
25 changes: 5 additions & 20 deletions config/routes.js → app/routes/users.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

module.exports = function(app, passport, auth) {

// User Routes
var users = require('../app/controllers/users');
// User routes use users controller
var users = require('../controllers/users');

module.exports = function(app, passport) {

app.get('/signin', users.signin);
app.get('/signup', users.signup);
app.get('/signout', users.signout);
Expand Down Expand Up @@ -62,20 +63,4 @@ module.exports = function(app, passport, auth) {
failureRedirect: '/signin'
}), users.authCallback);


// Article Routes
var articles = require('../app/controllers/articles');
app.get('/articles', articles.all);
app.post('/articles', auth.requiresLogin, articles.create);
app.get('/articles/:articleId', articles.show);
app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update);
app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy);

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

// Home route
var index = require('../app/controllers/index');
app.get('/', index.render);

};
6 changes: 3 additions & 3 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ module.exports = function(app, passport, db) {
})
}));

// Connect flash for flash messages
app.use(flash());

// Dynamic helpers
app.use(helpers(config.app.name));

// Use passport session
app.use(passport.initialize());
app.use(passport.session());

// Connect flash for flash messages
app.use(flash());

// Routes should be at the last
app.use(app.router);

Expand Down
35 changes: 0 additions & 35 deletions config/middlewares/authorization.js

This file was deleted.

21 changes: 19 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';

// Initializing system variables
var config = require('./config/config'),
auth = require('./config/middlewares/authorization'),
mongoose = require('mongoose');

// Bootstrap db connection
Expand Down Expand Up @@ -51,7 +50,25 @@ var app = express();
require('./config/express')(app, passport, db);

// Bootstrap routes
require('./config/routes')(app, passport, auth);
var routes_path = __dirname + '/app/routes';
var walk = function(path) {
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js$|coffee$)/.test(file)) {
require(newPath)(app, passport);
}
// We skip the app/routes/middlewares directory as it is meant to be
// used and shared by routes as further middlewares and is not a
// route by itself
} else if (stat.isDirectory() && file !== 'middlewares') {
walk(newPath);
}
});
};
walk(routes_path);


// Start the app by listening on <port>
var port = process.env.PORT || config.port;
Expand Down

0 comments on commit d59bf86

Please sign in to comment.