Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I'm trying to establish in authentication to a small web app using passport, mongodb, mongoose, express and the passport-local-mongoose plugin. I am being returned to a bad request when trying to log in to a user. I can register a user and get the data into the DB.

How do I proceed forward to debug this with error handling? The docs for passport and passport-local-mongoose seems light (and I'm a beginner).

App.js

app.use(passport.initialize())

app.use(passport.session())

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

var User = require('./models/user.js')

passport.use(new LocalStrategy(User.authenticate()));

//passport.use(new LocalStrategy(UserSchema.authenticate()))

passport.serializeUser(User.serializeUser());

passport.deserializeUser(User.deserializeUser());

// Connect to Mongoose

mongoose.connect('mongodb://localhost/users')

Registration route (gets data to the db, but fails to redirect)

// Register a user to the DB

router.post('/register', function(req, res, next){

  let firstName = req.body.firstName

  let lastName = req.body.lastName

  let username = req.body.email

  //let password = req.body.password

  let homeAirport = req.body.homeAirport

  User.register(new User ({

    firstName: firstName,

    lastName: lastName,

    username: username,

    homeAirport: homeAirport

  }),

    req.body.password, function(err, user) {

      if (err) {

        console.log(err)

        return res.render('register', {

          user: user

        })

      }

      // both of these works

      passport.authenticate('local', { failureRedirect: '/' }),

      function(req, res, next) {

        res.redirect('/');

      }

  })

})

Login Route (returns a bad request)

router.post('/login', passport.authenticate('local'), function(req, res) {

  res.redirect('/');

});

User Schema

var mongoose = require('mongoose'),

  Schema = mongoose.Schema,

  passportLocalMongoose = require('passport-local-mongoose');

// Define the scheme

var User = new Schema ({

  firstName: {

    type: String,

    index: true

  },

  lastName: {

    type: String,

    index: true

  },

  email: {

    type: String,

    index: true

  },

  homeAirport: {

    type: String,

    index: true

  }

})

User.plugin(passportLocalMongoose)

module.exports = mongoose.model('User', User)

1 Answer

0 votes
by (107k points)
edited by

As far as I have examined your query, the problem was with a passport, not with the passport-local-mongoose. It considers the http request to hold the fields username and password. So if you are applying an email for registration, it will definitely return the corrupt requests.

For fixing your problem, you just have to change the userNameField as illustrated below:

passport.use(new LocalStrategy({

    usernameField: 'email',

  },User.authenticate()));

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...