3

i have a collection like this in mongodb

{
   username:silver,
   Email:[email protected],
   password:silvester,
}

so to authenticate i will fetch data from database and then i will check given email is exist or not with if statement like this

app.post("/login",function(req,res){

   var email=req.body['emailid'];

   collection.find({email:[email protected]}).toArray(function(err,res)
   {
      if(res.length==0){
         console.log("name is not exist");
      }else{
         if(res.email==email){
            console.log("email is exist");
         }else{
            console.log("not exist");
         }
      }
   });
});

so here how to use passport module for authentication.let me know it with sample code with configuration. i am using express3.x framework .so how to configure it also.

1 Answer 1

3

Here you can read about local strategies, and here about configure.

Your local strategy should look like this:

passport.use(new LocalStrategy({
        emailField: 'email',
        passwordField: 'passw',
    },

    function (emailField, passwordField, done) {
        process.nextTick(function () {
            db.collection(dbCollection, function (error, collection) {
                if (!error) {
                    collection.findOne({
                        'email': [email protected]
                        'password': silvester // use there some crypto function
                    }, function (err, user) {
                        if (err) {
                            return done(err);
                        }
                        if (!user) {
                            console.log('this email does not exist');
                            return done(null, false);
                        }
                        return done(null, user);
                    });
                } else {
                    console.log(5, 'DB error');
                }
            });
        });
    }));
Sign up to request clarification or add additional context in comments.

6 Comments

should i write above coding within post request like app.post('/login',function(req,res){passport.use(new LocalStrategy({ emailField: 'email', passwordField: 'passw', }, }) let me know it..
I'm not sure that's a good idea. Also, do not forget to about passport.serializeUser and passport.deserializeUser.
how to use passport for authenticate the data of mongodb.
In your app.post, you can use passport.authenticate. All of this information is in the documentation
my doubt is after give login data like emailid and password i will post that to server by using app.post() so i will keep that value in different variable in post method after how to pass that data by using passport.use() or passport.authenticate() and then how to check that data exist is database (i am not telling about query,i am asking that where i have to write that query and how to comapre that data) i am not a familiar for node.js so let me know it.thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.