I am a complete newbie in node.js/express/mongodb stack in creating a REST api. Though the tutorials have been rather helpful to get me started, I find myself completely stuck when it comes to applying authorization/authentication to the api. Any examples as such would be really helpful. I've gone through connect and passport but there isn't a good example of an app where a user login/sign up(with authentication) + skeletal rest api to access data of the user is given. It would be rather awesome if anything like that can be provided?
Thank you!
Here is an example of authentication using passportjs
https://github.com/jaredhanson/passport-local/blob/master/examples/login/app.js
Hopefully this may help you.
For using it with mongodb
do retrieve all the records and store it in array using the toArray()
method.For eg in the code snippet in the example just modify it as
findByUsername(username, function(err, user) {
db.collections.find().toArray(function(err,user){
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
if (user.password != password) { return done(null, false, { message: 'Invalid password' }); }
return done(null, user);
})
});
}
});
));