Search code examples
javascriptnode.jsexpresspassport.jspassport-local

Passport not successfully redirecting


I took a tutorial from the odin project on passport and it worked so I decided to implement what I learned on my own project, the difference between theirs and mine is that I was following a design pattern while they weren't and I can't seem to find the problem as to why it is not responding . in my login page I use the email and password and I've tried changing the below code many times.

this is the controller.js

    //passport functions
passport.use(
  new LocalStrategy((username, password, done) => {
    User.findOne({ username: username }, (err, user) => {
      console.log(user)
      
      if (err) { 
        return done(err);
      };
      if (!user) {
        return done(null, false, { msg: "Incorrect username" });
      }
      if (user.password !== password) {
        return done(null, false, { msg: "Incorrect password" });
      }
      return done(null, user);
    });
  })
);
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});
//end ofpassport functions

    app.use(session({ secret: "cats", 
    resave: false, saveUninitialized: true }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.urlencoded({ extended: false }));
    app.use(function(req, res, next) {
      res.locals.currentUser = req.user;
      next();
    });

I then called the controller from the routes.js to authenticate the user yet it keeps failing and defaulting on the failredirecting

app.post('/signin', passport.authenticate("local",{
    successRedirect:"/",
    failureRedirect:'/signup'}))

Solution

  • By default, LocalStrategy expects to find credentials in parameters named username and password. If your site prefers to name these fields differently, options are available to change the defaults.

    name of parameters in req.body should match with custom fields and LocalStrategy not called when there are empty fields usernameField and passwordField

    const customFields = {
        usernameField : "email",
        passwordField : "password"
    }
    
    passport.use(
      new LocalStrategy(customFields ,(username, password, done) => {
        User.findOne({ username: username }, (err, user) => {
          console.log(user)
          
          if (err) { 
            return done(err);
          };
          if (!user) {
            return done(null, false, { msg: "Incorrect username" });
          }
          if (user.password !== password) {
            return done(null, false, { msg: "Incorrect password" });
          }
          return done(null, user);
        });
      })
    );
    passport.serializeUser(function(user, done) {
      done(null, user.id);
    });
    
    passport.deserializeUser(function(id, done) {
      User.findById(id, function(err, user) {
        done(err, user);
      });
    });
    //end ofpassport functions
    
        app.use(session({ secret: "cats", 
        resave: false, saveUninitialized: true }));
        app.use(passport.initialize());
        app.use(passport.session());
        app.use(express.urlencoded({ extended: false }));
        app.use(function(req, res, next) {
          res.locals.currentUser = req.user;
          next();
        });