Skip to content

Commit ad4da56

Browse files
committed
Add a middleware function to restrict access
- Add ```isAdmin``` or ```isUser``` as a middleware function in requests to block access. I couldn't do it because I don't really know what to restrict. :P - Improve signup strategy so that users and admins can't have same username.
1 parent 570cd61 commit ad4da56

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

passport_config.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ module.exports = function(app, passport){
5050
return cb(null, false, {message:"User already registered"});
5151
}
5252
else{
53-
app.db.users.insert({username: username, password:password});
53+
// Must not be the same as admins
54+
app.db.admins.findOne({username: username}, function(err, user) {
55+
if (err) {
56+
return cb(err);
57+
}
58+
if (user) {
59+
return cb(null, false, {message:"User already registered"});
60+
}
61+
else{
62+
app.db.users.insert({username: username, password:password});
63+
}
64+
});
5465
}
5566
});
5667
}else

routes/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ function rescanItem(req) {
364364
app.db.songs.find({ _id: { $in: items }}, function(err, songs) {
365365
if (!err && songs)
366366

367-
// add the location to the list of songs to scan
367+
// add the location to the list of songs to scan
368368
for (var i = 0; i < songs.length; i++) {
369369
songLocArr.push(songs[i].location);
370370
}
@@ -655,3 +655,31 @@ function getYoutubeSongs(req) {
655655
});
656656
});
657657
}
658+
659+
function isUser(req, res, next) {
660+
if (req.isAuthenticated()) {
661+
app.db.users.findOne({username: req.user.username}, function (err, user) {
662+
if (!user) {
663+
res.redirect('/login');
664+
} else {
665+
return next();
666+
}
667+
});
668+
}else {
669+
res.redirect('/login?notAuth');
670+
}
671+
}
672+
673+
function isAdmin(req, res, next) {
674+
if (req.isAuthenticated()) {
675+
app.db.admin.findOne({username: req.user.username}, function (err, user) {
676+
if (!user) {
677+
res.redirect('/admin');
678+
} else {
679+
return next();
680+
}
681+
});
682+
}else {
683+
res.redirect('/admin?notAuth');
684+
}
685+
}

0 commit comments

Comments
 (0)