0

So I'm trying to implement multiple local strategies into my nodejs/express app using passportjs. I followed this question and this issue but for some reason while I use req.user after second strategy it doesn't exists. Even though the process of finding user ('child' in my case) was done correctly. So this is my passportjs configure file:

const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
const Child = require('../models/child');
const config = require('../config/database');
const bcrypt = require('bcryptjs');

module.exports = function(passport){
  // Local Strategy
    passport.use('user', new LocalStrategy(function(username, password, done){
        // Match Username
        let query = {username:username};
        User.findOne(query, function(err, user){
            if(err) throw err;
            if(!user){
                return done(null, false, {message: 'No user found'});
            }

            // Match Password
            bcrypt.compare(password, user.password, function(err, isMatch){
                if(err) throw err;
                if(isMatch){
                    return done(null, user);
                } else {
                    return done(null, false, {message: 'Wrong password'});
                }
            });
        });
    }));

    passport.use('child', new LocalStrategy(function(username, password, done){
        // Match Username
        let query = {login:username};
        Child.findOne(query, function(err, user){
            if(err) throw err;
            if(!user){
                return done(null, false, {message: 'No user found1'});
            }

            // Match Password
            bcrypt.compare(password, user.password, function(err, isMatch){
                if(err) throw err;
                if(isMatch){
                    return done(null, user);
                } else {
                    return done(null, false, {message: 'Wrong password'});
                }
            });
        });
    }));

  passport.serializeUser(function(user, done) {
      var key;
      if(user instanceof User) {
          key = {
              id: user.id,
              type: 1
          }
      } else if (user instanceof Child) {
          key = {
              id: user.id,
              type: 2
          }
      }
    done(null, key);
  });

  passport.deserializeUser(function(key, done) {
      if(key.type === 1){
          User.findById(id, function(err, user) {
              done(err, user);
          });
      } else if (key.type === 2){
          Child.findById(id, function(err, user) {
              done(err, user);
          });
      }
  });
}

This is my post routes where I use the strategies:

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

router.post('/childlogin',
  passport.authenticate('child', {successRedirect: '/childDashboard', failureRedirect: '/', failureFlash: true}),
  function(req, res) {
    res.redirect('/childDashboard');
  });

And for some reason when I login as user everything is ok. I can access req.user from /dashboard. But when I try to login as child req.user is undefinded. Even though strategy is working. I know that it is working because if I do "console.log(user)" right before "return done(null, user)" in child's strategy I get the full info about the right object. It just seems that object is not pushed and defined later in app. Does anyone have an idea what I might do wrong? I'm quite new to nodejs/express so I won't be surprised if it's something stupid.

Fubundzer
  • 75
  • 2
  • 9
  • Could you provide the `User` and `Child` schemas? – Leandro Rodrigues Jun 16 '17 at 03:00
  • Okay I solved my problem. In `deserializeUser` method I used `id` instead of `key.id`. Also I had another passport configuration for `User` somewhere in my code and I guess that's why it passed for this schema. I guess that I have to keep my code clean. – Fubundzer Jun 16 '17 at 16:48

0 Answers0