Vishnu Mehta
Vishnu Mehta

Reputation: 51

PASSPORT AUTHENTICATION USING EXPRESS NOT RESPONDING

I am trying to use passport authentication using nodejs and mongodb, below is the code, what I have tried, not getting any response for this. Its not even entering the findOrCreate method.

I am using nodejs v: 10.16.3, npm v: 6.9.0

mongodb version:4.2.0 and Operating system is MAC OS Mojave.

Here is the list of dependencies-

"dependencies": {
    "body-parser": "^1.19.0",
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.4.4",
    "express": "^4.17.1",
    "express-session": "^1.16.2",
    "flash": "^1.1.0",
    "http": "0.0.0",
    "mongo": "^0.1.0",
    "mongodb": "^3.3.2",
    "mongoose": "^4.3.5",
    "mysql": "^2.17.1",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0"
  }

The passport code is as followed-

 app.post("/signup",(req,res)=>{
    console.log("Inside Signup",req.body);
    res.send(req.body);
    var emails=req.body.Email;
    console.log(emails);
    var names=req.body.Name;
    console.log(names);
    var passwords=req.body.Password;
   console.log(passwords);
    si(names,emails,passwords);
  });
 function si(username,email,password){
      passport.use('signup', new LocalStrategy({
        passReqToCallback : true
      },
      function(req, username, password, done) {
        findOrCreateUser = function(){
          // find a user in Mongo with provided username
    console.log("Step1 success");
          console.log(username);
          console.log(email);
          console.log(password);
          var dbo = db.db("record");
          dbo.collection("records").findOne({'Username':username}, function(err, result) {
            if (err){
              console.log('Error in SignUp: '+err);
              return done(err);
            }
            // already exists
            if (result) {
              console.log('User already exists');
              return done(null, false, 
                 req.flash('message','User Already Exists'));
            } else {
              var newUser = new User();
              newUser.Username = username;
              newUser.Password = password;
              newUser.Email = email;
              newUser.save(function(err) {
                if (err){
                  console.log('Error in Saving user: '+err);  
                  throw err;  
                }
                console.log('User Registration succesful');    
                return done(null, newUser);
              });
            }
          });
        };

        // Delay the execution of findOrCreateUser and execute 
        // the method in the next tick of the event loop
        process.nextTick(findOrCreateUser);
      }))
};

Can anyone suggest, how I can make it work?

Upvotes: 1

Views: 89

Answers (1)

Thomas Reichmann
Thomas Reichmann

Reputation: 467

Try creating the user by passing an object to the constructor instead of changing the properties individually.

newUser = new User({ username: username, email: emal, password: password });

Upvotes: 1

Related Questions