jeudi 16 juin 2016

Multiple Serialize User in PassportJs using MySQL

Am developing application using Nodejs with MySQL..

For login Authentication am using passportJS. There are two login in this application one for admin and another one for customer. so seperate tables is there are USERS and REGISTERS.

passport.serializeUser(function(user, done) {
  done(null, {
     id      : user.id,
     isAdmin : user.isAdmin // or some other property/check
 });
});

    // used to deserialize the user


passport.deserializeUser(function(user, done) {
var table = user.isAdmin ? 'register' : 'users';
connection.query('select * from ?? where id = ?', [ table, user.id ], function(err, rows) {
    if (err) {
        return done(err);
    } else if (! Array.isArray(rows) || ! rows.length) {
        return done();
    } else {
        return done(null, rows[0]);
    }
});

});

In deserializeuser if i logging In with customer id...its checking with user table for same id...so am getting wrong data

Question Updated: Local-Login for Admin

passport.use('local-login', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback
    },
    function(req, email, password, done) { // callback with email and password from our form

        connection.query("select * from users WHERE email = '" + email + "'",function(err,rows){
            if (err)
                return done(err);
            if (!rows.length) {
                return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
            } 
            // if the user is found but the password is wrong
            if (!( rows[0].password == password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
            // all is well, return successful user
            return done(null, rows[0]);         

        });

    }));

Customer-Login

  passport.use('customer-login', new LocalStrategy({
       usernameField : 'mobile',
       passwordField : 'otp',
       passReqToCallback : true 
  },
function(req, mobile, otp, done) { 

connection.query("select * from register WHERE mobile = '" + mobile + "'",function(err,rows){
    if (err)
        return done(err);
    if (!rows.length) {
        return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
    } 
    // if the user is found but the password is wrong
    if (!( rows[0].otp == otp))
        return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
    // all is well, return successful user
    console.log(rows);
    return done(null, rows[0]);         

});

}));

For admin am using email as username for login

For customer am using mobile number for login

register registertable

users user table

Aucun commentaire:

Enregistrer un commentaire