Skip to content

Commit

Permalink
Added an admin login filter (to avoid DRY) (commit by emerleite, but …
Browse files Browse the repository at this point in the history
…failed merging).
  • Loading branch information
pedrofranceschi committed Dec 11, 2010
1 parent 6822e8d commit 92956b8
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions blogode.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ app.get("/", function(req, res){
}); });
}); });


app.get("/admin", function(req, res){
// return admin panel function adminLoginFilter(req, res, next) {
// verifies if user is an admin


if(!req.session.username) { if(!req.session.username) {
return res.redirect("/admin/login"); return res.redirect("/admin/login");
} }
next();
}

app.get("/admin", adminLoginFilter, function(req, res){
// return admin panel


res.render('admin/panel', { res.render('admin/panel', {
layout: false layout: false
Expand Down Expand Up @@ -77,12 +83,9 @@ app.post("/admin/authenticate", function(req, res){
}); });
}); });


app.get('/admin/posts', function(req, res) { app.get('/admin/posts', adminLoginFilter, function(req, res) {
// return the list of posts (as admin) // return the list of posts (as admin)


if(!req.session.username) {
return res.redirect("/admin/login")
}
posts.getPosts(0, function (posts){ posts.getPosts(0, function (posts){
res.render('admin/posts/index', { res.render('admin/posts/index', {
layout: false, layout: false,
Expand All @@ -91,23 +94,17 @@ app.get('/admin/posts', function(req, res) {
}); });
}); });


app.get('/admin/posts/new', function(req, res) { app.get('/admin/posts/new', adminLoginFilter, function(req, res) {
// return the formulary to create a new post // return the formulary to create a new post


if(!req.session.username) {
return res.redirect("/admin/login")
}
res.render('admin/posts/new', { res.render('admin/posts/new', {
layout: false layout: false
}); });
}); });


app.get('/admin/posts/:id', function(req, res) { app.get('/admin/posts/:id', adminLoginFilter, function(req, res) {
// return a post (to edit) // return a post (to edit)


if(!req.session.username) {
return res.redirect("/admin/login")
}
posts.getPost(req.param('id'), function (post){ posts.getPost(req.param('id'), function (post){
res.render('admin/posts/edit', { res.render('admin/posts/edit', {
layout: false, layout: false,
Expand All @@ -116,12 +113,9 @@ app.get('/admin/posts/:id', function(req, res) {
}); });
}); });


app.post('/admin/posts/save', function(req, res) { app.post('/admin/posts/save', adminLoginFilter, function(req, res) {
// saves a post // saves a post


if(!req.session.username) {
return res.redirect("/admin/login")
}
sys.puts('saving: ' + sys.inspect(req.param('textEditor'))) sys.puts('saving: ' + sys.inspect(req.param('textEditor')))
if(!req.param('title') || !req.param('body')) { if(!req.param('title') || !req.param('body')) {
return res.redirect("/admin/posts/new"); return res.redirect("/admin/posts/new");
Expand All @@ -131,12 +125,9 @@ app.post('/admin/posts/save', function(req, res) {
}); });
}); });


app.put('/admin/posts/:id', function(req, res) { app.put('/admin/posts/:id', adminLoginFilter, function(req, res) {
// updates a post // updates a post


if(!req.session.username) {
return res.redirect("/admin/login")
}
if(!req.param('title') || !req.param('body')) { if(!req.param('title') || !req.param('body')) {
return res.redirect("/admin/posts/new"); return res.redirect("/admin/posts/new");
} }
Expand All @@ -145,12 +136,9 @@ app.put('/admin/posts/:id', function(req, res) {
}); });
}); });


app.get('/admin/posts/destroy/:id', function(req, res) { app.get('/admin/posts/destroy/:id', adminLoginFilter, function(req, res) {
// destroys a post // destroys a post


if(!req.session.username) {
return res.redirect("/admin/login")
}
if(!req.param('id')) { if(!req.param('id')) {
return res.redirect("/admin/posts/"); return res.redirect("/admin/posts/");
} }
Expand Down

0 comments on commit 92956b8

Please sign in to comment.