-1

Yes, I see that this question has been asked here and here, but the answers point to either making changes to your deserializeUser callback, or changing something in your Mongoose model.

I've tried the first to no avail and am using the regular ol' NodeJS driver, so I'm not quite sure where to pinpoint the root cause of my issue.

Here's my script:

AUTHENTICATE.JS:

var app = require('express');
var router = app.Router();
var assert = require('assert');
var bcrypt = require('bcrypt');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

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

passport.use(new LocalStrategy({
  usernameField: 'email',
  passwordField: 'password'
},
  function(username, password, done) {
User.findOne({ username: username }, function(err, user) {
  if (err) { return done(err); }
  if (!user) {
    return done(null, false, { message: 'Incorrect username.' });
  }
  if (!user.validPassword(password)) {
    return done(null, false, { message: 'Incorrect password.' });
  }
  return done(null, user);
    });
  }
));

router.post('/login',
   passport.authenticate('local', {successRedirect:'/',   
     failureRedirect:'/login',failureFlash: false}),
      function(req, res) {
      res.redirect('/');
});

For my app.js file I've tried a number of the fixes suggested in similar questions but nothing has made a difference. This is what it looks like:

APP.JS:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressValidator = require('express-validator');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var mongo = require('mongodb').MongoClient;
var session = require('express-session');
var bcrypt = require('bcrypt');

// Express Session
app.use(session({
  secret: 'keyboard cat',
  saveUninitialized: true,
  resave: true,
  cookie: {secure: true}
}));


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

//initialize passport for app
app.use(passport.initialize());
app.use(passport.session());

// make mongodb available to the application
app.use((req, res, next) => {
  mongo.connect('mongodb://localhost:27017/formulas', (e, db) => {
  if (e) return next(e);
  req.db = db;
  next();
  });
});

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));

// Express Validator
app.use(expressValidator({
  errorFormatter: function(param, msg, value) {
      var namespace = param.split('.')
      , root    = namespace.shift()
      , formParam = root;

  while(namespace.length) {
      formParam += '[' + namespace.shift() + ']';
  }
  return {
      param : formParam,
      msg   : msg,
      value : value
    };
   }
  }));

//define routes here


app.set('port', (process.env.PORT || 3000));

app.listen(app.get('port'), function(){
  console.log("The server is now listening on port "+app.get('port'));
});

module.exports = app;

Any help would be greatly appreciated, thank you.

Community
  • 1
  • 1
Jonathan Bechtel
  • 3,497
  • 4
  • 43
  • 73
  • Did that work for you? do you still need help? – Josh Feb 24 '17 at 01:40
  • Thank you for your answer, and I apologize for the delayed response, but at the moment I'm still grokking through this. I understand how Mongoose allows you to create schemas, and you can use one for a User and then use that in `deserializeUser` but I'm not sure how the other example relates to that because the `User` class is never defined. For the moment I'm probably just going to create my own `User` object and see how that goes. – Jonathan Bechtel Feb 27 '17 at 00:16

1 Answers1

1

As both questions you included point out, you need to define the User class first before you can use it. It's not something that passportjs or expressjs provide, you need to implement it for yourself or use another module that gives you that functionality (like mongoose).

  • In the first SO link you shared the answer suggested the OP implement a user model (User) in mongoose (it seems to be a popular choice).
  • The second links answer simplified things a bit by adding a hard-coded object to represent the user in the deserializer function.
Josh
  • 3,264
  • 1
  • 23
  • 35