错误:无法将用户序列化为与Node Js,Passport和Mysql数据库的会话

时间:2018-07-11 05:44:35

标签: javascript mysql node.js passport.js

我将Mysql数据库用于Node Js应用程序。使用护照进行身份验证。这是我的Passport.js文件。

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   MoPubRewardedVideos.initializeRewardedVideo(this);
   MoPub.onCreate(this); // remove 
   // ...
}

@Override
public void onPause() {
    super.onPause();
    MoPub.onPause(this); // remove 
}

@Override
public void onResume() {
    super.onResume();
    MoPub.onResume(this);  // remove 
}

// The following methods are required for Chartboost rewarded video mediation
@Override
public void onStart() {
    super.onStart();
    MoPub.onStart(this); // remove 
}

@Override
public void onRestart() {
    super.onRestart();
    MoPub.onRestart(this); // remove 
}

@Override
public void onStop() {
    super.onStop();
    MoPub.onStop(this); // remove 
}

@Override
public void onDestroy() {
    super.onDestroy();
    MoPub.onDestroy(this); // remove 
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    MoPub.onBackPressed(this); // remove 
  }
}

根据我的应用程序,每当我在“注册”页面中创建新用户时,它就成功创建了用户,并通过调用Serialize and Deserialize函数创建了会话。

但是当我尝试登录用户时,它正在创建此错误。 登录过程中仅调用序列化功能,而未调用反序列化功能。

但是,如果我禁用带有session:false的Session,它将使我登录,但没有我不希望的Session。

这是我的路线文件。

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var mysql = require('mysql');

var connection = mysql.createConnection({
    host     : "localhost",
    user     : "root",
    password : "",
    database: "cafe"
});
connection.connect(function(err){
    if(err) throw err;

    else console.log("Passport Server Connected");
});


passport.serializeUser(function(user, done) {
    console.log("In Serialize !"+ user.ID);
    done(null, user.id);
});

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    connection.query("select * from user where id = "+id,function(err,rows){
        console.log("Inside Deserialize ---> "+rows[0]);
        done(err, rows[0]);
    });
});

// https://gist.github.com/manjeshpv/84446e6aa5b3689e8b84 
// Passport with mysql database

passport.use('local-signup', 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) {

      connection.query("select * from user where email = '"+email+"'",function(err,rows){
        console.log(rows);
        console.log("above row object");
        if (err)
            return done(err);
            if (rows.length > 0) {
            // return done(null, false, req.flash('signupMessage', 'That email is already taken.')); // Not Working
            return done(null, false, {message : 'Email Id Already Taken !'}); //Default Json Unauthorised
        } else {
            // if there is no user with that email
            // create the user

            var newUserMysql = new Object();
            newUserMysql.email    = email;
            newUserMysql.password = password; // use the generateHash function in our user model
            console.log(newUserMysql);
            var insertQuery = "INSERT INTO user ( email,password ) VALUES ('"+ email +"','"+ password +"')";
            console.log(insertQuery);
            connection.query(insertQuery,function(err,rows){
                newUserMysql.id = rows.insertId;
                if(err) throw err;
                // console.log("Error is "+ err);
                // console.log(insertQuery);
                return done(null, newUserMysql);
            });
        }   
    });
    // connection.end();
}));


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 `user` 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
            return done(null, false, {message: 'No User Found! '});
        } 

        // 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
            return done(null, false, {message: 'Oops! Wrong Password! '});
        }
        // all is well, return successful user
        console.log(" Inside callback of local-login -> "+rows[0]);
        return done(null, rows[0]);         
    });
}));

// module.exports;

如果我可能要问,我的闪光灯也不起作用。任何帮助将不胜感激。

PS-请注意,我的“注册”工作正常,因此基本上正在创建会话,并且序列化和反序列化功能也正常运行。问题仅在登录会话中

0 个答案:

没有答案