title: Passport.js: Cannot read property 'username' of undefined (Node)
body: <p>I'm trying to implement <code>passport-local</code> but I keep getting this error when I test it with Postman: <code>Cannot read property 'username' of undefined</code></p>
  
  <p>Here is my model, <code>User.js</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const mongoose = require('mongoose');
  const {Schema} = mongoose;
  const passportLocalMongoose = require('passport-local-mongoose');
  
  const User = new Schema({
    username: String,
   email: String,
    password: String,
    firstName: String,
    lastName: String
  });
  
  User.plugin(passportLocalMongoose);
  
  module.exports = mongoose.model('User', User);</code></pre>
  </div>
  </div>
  </p>
  
  <p>Routes: <code>routes/authRoutes.js</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const passport = require('passport');
  const User = require('../models/User.js');
  
  module.exports = app =&gt; {
    // POST to /register
    app.post('/api/register', (req, res) =&gt; {
      // Create a user object to save, using values from incoming JSON
      const newUser = new User({
        username: req.body.username,
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        email: req.body.email,
      });
  
      // Save, via passport's "register" method, the user
      User.register(newUser, req.body.password, (err, user) =&gt; {
        // If there's a problem, send back a JSON object with the error
        if (err) {
          return res.send(JSON.stringify({ error: err }));
        }
        // Otherwise, for now, send back a JSON object with the new user's info
        return res.send(JSON.stringify(user));
      });
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><code>index.js</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const express = require('express');
  const mongoose = require('mongoose');
  const passportLocalMongoose = require('passport-local-mongoose');
  const keys = require('./config/keys');
  const session = require('express-session');
  const passport = require('passport');
  const LocalStrategy = require('passport-local').Strategy;
  const MongoStore = require('connect-mongo')(session);
  
  mongoose.connect(keys.mongoURI);
  
  const app = express();
  
  app.use(require('express-session')({
    secret: 'secretsecret',
    resave: false,
    saveUninitialized: false
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  
  require('./routes/authRoutes')(app);
  
  // Configure Passport
  const User = require('./models/User');
  passport.use(new LocalStrategy(User.authenticate()));
  passport.serializeUser(User.serializeUser());
  passport.deserializeUser(User.deserializeUser());
  
  require('./routes/authRoutes')(app);
  
  const PORT = process.env.PORT || 5000;
  app.listen(PORT);</code></pre>
  </div>
  </div>
  </p>
  
  <p>And my <code>package.json</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
    "name": "server",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "engines": {
      "node": "8.1.1",
      "npm": "5.0.3"
    },
    "scripts": {
      "start": "node index.js",
      "dev": "nodemon index.js"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
      "connect-mongo": "^1.3.2",
      "express": "^4.15.4",
      "express-session": "^1.15.5",
      "mongoose": "^4.11.11",
      "passport": "^0.4.0",
      "passport-local": "^1.0.0",
      "passport-local-mongoose": "^4.2.1"
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I already checked other similar questions, but their solutions are not my solutions, most of them didn't initialize Passport.</p>
  
  <p><code>edit:</code> Postman logs:
  <code>POST http://localhost:5000/api/register</code>
  <code>{
    "username": "Test",
      "email": "testing@test.com",
    "password": "password",
    "firstName": "Testt",
    "lastName": "Teststs"
  }</code></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;title&gt;Error&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;pre&gt;TypeError: Cannot read property &amp;#39;username&amp;#39; of undefined
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at app.post (server/routes/authRoutes.js:10:25)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (server/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (server/node_modules/express/lib/router/route.js:137:13)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Route.dispatch (server/node_modules/express/lib/router/route.js:112:3)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (server/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at /server/node_modules/express/lib/router/index.js:281:22
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.process_params (/server/node_modules/express/lib/router/index.js:335:12)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (/server/node_modules/express/lib/router/index.js:275:10)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at SessionStrategy.strategy.pass (/server/node_modules/passport/lib/middleware/authenticate.js:338:9)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at SessionStrategy.authenticate (/server/node_modules/passport/lib/strategies/session.js:75:10)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at attempt (/server/node_modules/passport/lib/middleware/authenticate.js:361:16)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at authenticate (/server/node_modules/passport/lib/middleware/authenticate.js:362:7)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (/server/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at trim_prefix (/server/node_modules/express/lib/router/index.js:317:13)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at /server/node_modules/express/lib/router/index.js:284:7
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.process_params (/server/node_modules/express/lib/router/index.js:335:12)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at next (/server/node_modules/express/lib/router/index.js:275:10)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at initialize (/server/node_modules/passport/lib/middleware/initialize.js:53:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Layer.handle [as handle_request] (/server/node_modules/express/lib/router/layer.js:95:5)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at trim_prefix (/server/node_modules/express/lib/router/index.js:317:13)
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at /server/node_modules/express/lib/router/index.js:284:7
              &lt;br&gt; &amp;nbsp; &amp;nbsp;at Function.process_params (/server/node_modules/express/lib/router/index.js:335:12)
          &lt;/pre&gt;
      &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>