title: passport js deserialize user, variable undefined
body: <p>I'm trying to build a backend for a web app, but I'm stuck on passport. Right now I can register a user and log that user in, but I can not log in twice in a row. Whenever deserialize user is called I get "TypeError: Cannot read property 'findOne' of undefined" I see that user is undefined but it is defined when I call it on login() and signup()</p>
  
  <pre><code>var login = require('./login');
  var signup = require('./signup');
  
  module.exports = function (passport: any, user: any) {
  
  // Passport needs to be able to serialize and deserialize users to support persistent login sessions
  passport.serializeUser(function (user: any, done: any) {
      done(undefined, user.username);
  });
  
  // deserialize user 
  passport.deserializeUser(function (id: any, done: any) {
  
      user.findOne(id).then(function (user: any) {
  
          if (user) {
              done(null, user.get());
          } else {
              done("error", undefined);
          }
      });
  });
  
  // Setting up Passport Strategies for Login and SignUp/Registration
  login(passport, user);
  signup(passport, user);
  
  }
  </code></pre>
  
  <p>in my app.ts file i implement the strategies like this:</p>
  
  <pre><code>var models = require("./config/models/User");
  require('./config/passport/init')(passport, models.user);
  </code></pre>
  
  <p>signup.js</p>
  
  <pre><code>var LocalStrategy   = require('passport-local').Strategy;
  
  var bCrypt = require('bcrypt-nodejs');
  
  module.exports = function(passport : any, user : any) {
  
  var User = user
  
  passport.use('signup', new LocalStrategy(
  {
      usernameField: 'username',
      passwordField: 'password',
      passReqToCallback: true // allows us to pass back the entire request to the callback
  
  }, function(req: Request, username: string, password: string, done: any) {
  
      var User = user
  
      console.log("signup"+User)
  
      var generateHash = function(password: string) {
          return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);        
      };
  
      User.findOne({
          where: {
              username: username
          }
      }).then(function(user: any) {
  
          if (user)     
          {
              console.log('already taken')
              return done(null, false, {
                  message: 'That email is already taken'
              });
          } else {
  
              var userPassword = generateHash(password);
              var data =
                  {
                      username: username,
                      password: userPassword,
                      firstname: req.body.firstname,
                      lastname: req.body.lastname
                  };
  
                  User.create(data).then(function(newUser: any, created: boolean) {
  
                  if (!newUser) {      
                      return done(null, false);
                  }
  
                  if (newUser) {
                      return done(null, newUser);
                  }
              });    
          }     
      });
  }
  
  ));
  
  }
  </code></pre>
  
  <p>login.js</p>
  
  <pre><code>import { passport } from '../../app'
  var LocalStrategy   = require('passport-local').Strategy;
  var bCrypt = require('bcrypt-nodejs');
  
  
  module.exports = function(passport : any, user : any) {
  
  //LOCAL SIGNIN
  passport.use('login', new LocalStrategy(
  
  {
  
      // by default, local strategy uses username and password, we will override with email
  
      usernameField: 'username',
  
      passwordField: 'password',
  
      passReqToCallback: true // allows us to pass back the entire request to the callback
  
  },
  
  
  function(req : Request, username : string, password : string, done : any) {
  
      var User = user;
  
      console.log("login: "+User)
  
      var isValidPassword = function(userpass : string, password : string) {
  
          return bCrypt.compareSync(password, userpass);
  
      }
  
      User.findOne({
          where: {
              username: username
          }
      }).then(function(user : any) {
  
          if (!user) {
  
              return done(null, false, {
                  message: 'User does not exist'
              });
  
          }
  
          if (!isValidPassword(user.password, password)) {
  
              return done(null, false, {
                  message: 'Incorrect password.'
              });
  
          }
  
  
          var userinfo = user.get();
          return done(null, userinfo);
  
  
      }).catch(function(err : Error) {
  
          console.log("Error:", err);
  
          return done(null, false, {
              message: 'Something went wrong with your Signin'
          });
  
      });
  
  
  }
  
  ));
  
  }
  </code></pre>