Support for multiple user types by Passport-local mongoose node.js

I wanted two types of users logging in (User, Client). How exactly can I create localStrategies, serialize and deserialize user for both types in my app.js I have two separate schemas, both using the PassportLocalMongoose plugin.

I currently configuraing my passport like below,

var User = require('./models/User');
var Client= require('./models/Client');
passport.use(new LocalStrategy(User.authenticate(), Client.authenticate()));
passport.serializeUser(User.serializeUser(), Client.serializeUser());
passport.deserializeUser(User.deserializeUser(), Client.deserializeUser());

The problem lies when I try to register. Registering as a User works, but as a client shows "Unauthorized" error. How can I fix this problem?

After going through the documentation of passport.js (kudos to Jared), I understood that I was doing almost everything wrong.

  1. Created two localStrategies

    passport.use('userLocal', new LocalStrategy(User.authenticate())); passport.use('clientLocal', new LocalStrategy(Client.authenticate()));

and to authenticate,

passport.authenticate('userLocal')(req, res, function () {
    res.redirect('/profile');
  });
and
passport.authenticate('clientLocal')(req, res, function () {
    res.redirect('/client');
  });
  1. Used passport module (l=not using the passport-local-mongoose module) for serializeUser and deseriealizeUser.

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

    passport.deserializeUser(function(user, done) { if(user!=null) done(null,user); });

The whole user schema (object) is now stored in the request and can be accessed through any of your routes.

Hope it helps out others with a similar issue.

saintedlama/passport-local-mongoose: Passport-Local , Passport-Local Mongoose is a Mongoose plugin that simplifies building username for user authentication in his blog post User Authentication With Passport.js models/user'); // use static authenticate method of model in LocalStrategy use static serialize and deserialize of model for passport session support passport. This is because passport-local-mongoose doesn’t need it. Here we did not add any methods to hash our password or to compare our passwords as we do normally for authentication because passport-local-mongoose will do all that for us. 3.Configure Passport/Passport-Local in app.js : In app.js first, you have to initialize the passport

You can make role in mongoose schema and give it to user or client. Based on the role you can do the authentication

Multiple "local" strategies � Issue #50 � jaredhanson/passport � GitHub, Also, I'm not very proficient in passport or node, so please pardon me if I say something Anyway, I'm trying to implement an kind of online "mini shopping mall", using express, sequelize over mysql and, ofc, passport. Support for Two types of Users login saintedlama/passport-local-mongoose#240. This uses the passport-local-mongoose register method to salt the password for us. We just have to pass it in in plain text. We just have to pass it in in plain text. Now we run node index.js .

you need to create two strategy and edit your serialize and deserialize function for support multiple formats When you call authenticate() you can specify startegy name

for example In middleware

exports.authenticateUserA = function (req, res, next) {
    console.log('authenticateA', req.body.hostname)
    passport.authenticate('loginA', {failureRedirect: '/login-fail',successRedirect: "/home"}, function (err, player, info) {}})

In passport-config.js

  passport.serializeUser(function (user, done) {
     console.log('deserialize user ')
     if (typeof user.mac_address == 'undefined') {

      user.data_type = 'userTypeA'
      done(null, user);
     } else{
        user.data_type = 'userTypeB'
        done(null, user);
    }

    });

User Authentication(with Node.js and MongoDB), models/user"), LocalStrategy = require("passport-local"), passportLocalMongoose = require("passport-local-mongoose")var app = express (); By plugging into Passport, local authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express. Install $ npm install passport-local Usage Configure Strategy. The local authentication strategy authenticates users using a username and password.

How to restrict access using Passport.js role-based authorisation, Learn how to assign roles to your users so as to restrict access to certain Build an authentication system using Node.js, Express, and Passport.js � How to layout, and we added MongoDB and Mongoose for back-end data storage. Finally, we added support for Passport.js Google OAuth strategy and� Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

Local Authentication Using Passport in Node.js, Paul Orac shows how Passport, Node.js, Express, and MongoDB consult our quick tip, “Install Multiple Versions of Node.js Using nvm”. database work together to keep track of a user's authentication state. We'll also be using passport-local-mongoose, which will simplify the Go back to it and type: Next, we use the Mongoose API to find the User in our underlying collection of Users to check if the user is a valid user or not. The last parameter in our callback : done denotes a useful method using which we could signal success or failure to Passport module.

Understanding Sessions and Local Authentication in Express with , mongoose – for MongoDb object modeling; express-session – for user input type = "submit" > will be created for every unique user across multiple HTTP requests. It is mostly used to support authentication sessions over HTTP. passport-local-mongoose takes care of salting and hashing user� Building an authentication system in Node.js can be challenging, especially for integrating OAuth2.0 and authenticated API-based endpoints. Passport.js is great for connecting services and for a base to integrate OAuth, but getting Passport.js set up for an existing Mongoose User model can be a little challenging.