Skip to content

Commit

Permalink
fixed route-separation example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 25, 2010
1 parent 6543a3d commit f9d2432
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 62 deletions.
68 changes: 34 additions & 34 deletions examples/route-middleware/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@ var app = express.createServer();

// Dummy users
var users = [
{ id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' },
{ id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' },
{ id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
{ id: 0, name: 'tj', email: 'tj@vision-media.ca', role: 'member' }
, { id: 1, name: 'ciaran', email: 'ciaranj@gmail.com', role: 'member' }
, { id: 2, name: 'aaron', email: 'aaron.heckmann+github@gmail.com', role: 'admin' }
];

function loadUser(req, res, next) {
// You would fetch your user from the db
var user = users[req.params.id];
if (user) {
req.user = user;
next();
} else {
next(new Error('Failed to load user ' + req.params.id));
}
// You would fetch your user from the db
var user = users[req.params.id];
if (user) {
req.user = user;
next();
} else {
next(new Error('Failed to load user ' + req.params.id));
}
}

function andRestrictToSelf(req, res, next) {
// If our authenticated user is the user we are viewing
// then everything is fine :)
if (req.authenticatedUser.id == req.user.id) {
next();
} else {
// You may want to implement specific exceptions
// such as UnauthorizedError or similar so that you
// can handle these in app.error() specifically
// (view ./examples/pages for this)
next(new Error('Unauthorized'));
}
// If our authenticated user is the user we are viewing
// then everything is fine :)
if (req.authenticatedUser.id == req.user.id) {
next();
} else {
// You may want to implement specific exceptions
// such as UnauthorizedError or similar so that you
// can handle these in app.error() specifically
// (view ./examples/pages for this)
next(new Error('Unauthorized'));
}
}

function andRestrictTo(role) {
return function(req, res, next) {
if (req.authenticatedUser.role == role) {
next();
} else {
next(new Error('Unauthorized'));
}
return function(req, res, next) {
if (req.authenticatedUser.role == role) {
next();
} else {
next(new Error('Unauthorized'));
}
}
}

// Middleware for faux authentication
Expand All @@ -62,24 +62,24 @@ function andRestrictTo(role) {
// may interacte with middleware

app.use(function(req, res, next){
req.authenticatedUser = users[0];
next();
req.authenticatedUser = users[0];
next();
});

app.get('/', function(req, res){
res.redirect('/user/0');
res.redirect('/user/0');
});

app.get('/user/:id', loadUser, function(req, res){
res.send('Viewing user ' + req.user.name);
res.send('Viewing user ' + req.user.name);
});

app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){
res.send('Editing user ' + req.user.name);
res.send('Editing user ' + req.user.name);
});

app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){
res.send('Deleted user ' + req.user.name);
res.send('Deleted user ' + req.user.name);
});

app.listen(3000);
Expand Down
7 changes: 1 addition & 6 deletions examples/route-separation/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@ var posts = [
];

exports.list = function(req, res){
res.render('post/list', {
locals: {
title: 'Posts'
, posts: posts
}
});
res.render('post/list', { title: 'Posts', posts: posts });
};
6 changes: 1 addition & 5 deletions examples/route-separation/site.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

exports.index = function(req, res){
res.render('index', {
locals: {
title: 'Route Separation Example'
}
});
res.render('index', { title: 'Route Separation Example' });
};
25 changes: 8 additions & 17 deletions examples/route-separation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
// Fake user database

var users = [
{ name: 'TJ', email: 'tj@vision-media.ca' }
, { name: 'Tobi', email: 'tobi@vision-media.ca' }
];
{ name: 'TJ', email: 'tj@vision-media.ca' }
, { name: 'Tobi', email: 'tobi@vision-media.ca' }
];

exports.list = function(req, res){
res.render('user/list', {
locals: {
title: 'Users'
, users: users
}
});
res.render('user/list', { title: 'Users', users: users });
};

exports.load = function(req, res, next){
Expand All @@ -27,19 +22,15 @@ exports.load = function(req, res, next){

exports.view = function(req, res){
res.render('user/view', {
locals: {
title: 'Viewing user ' + req.user.name
, user: req.user
}
title: 'Viewing user ' + req.user.name
, user: req.user
});
};

exports.edit = function(req, res){
res.render('user/edit', {
locals: {
title: 'Editing user ' + req.user.name
, user: req.user
}
title: 'Editing user ' + req.user.name
, user: req.user
});
};

Expand Down

0 comments on commit f9d2432

Please sign in to comment.