Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 护照注册只是不断旋转_Node.js_Express_Passport.js - Fatal编程技术网

Node.js 护照注册只是不断旋转

Node.js 护照注册只是不断旋转,node.js,express,passport.js,Node.js,Express,Passport.js,我正在使用Passportjs,在我注册并发送post请求后,我再也没有收到回复。以下是我正在使用的passport配置: // config/passport.js require('dotenv').config(); // load all the things we need var LocalStrategy = require('passport-local').Strategy; // load up the user model var mysql = requir

我正在使用Passportjs,在我注册并发送post请求后,我再也没有收到回复。以下是我正在使用的passport配置:

    // config/passport.js
require('dotenv').config();
// load all the things we need
var LocalStrategy   = require('passport-local').Strategy;

// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcryptjs');
var connection = mysql.createConnection({
    host: process.env.DBHOST,
    user: process.env.DBUSER,
    password: process.env.DBPASS,
    port: process.env.DBPORT,
    database: process.env.DB
  });

// expose this function to our app using module.exports
module.exports = function(passport) {

    // =========================================================================
    // passport session setup ==================================================
    // =========================================================================
    // required for persistent login sessions
    // passport needs ability to serialize and unserialize users out of session

    // used to serialize the user for the session
    passport.serializeUser(function(user, done) {
        done(null, user);
    });

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        getUser(id, function (response) {
            done(err, response);
        })        
    });

    // =========================================================================
    // LOCAL SIGNUP ============================================================
    // =========================================================================
    // we are using named strategies since we have one for login and one for signup
    // by default, if there was no name, it would just be called 'local'

    passport.use(
        'local-signup',
        new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'email',
            passwordField : 'pass',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        async function(req, email, password, done) {
            // find a user whose email is the same as the forms email
            // we are checking to see if the user trying to login already exists

            userExists(email, async function(resp) {
                console.log(resp)
                if (resp) {
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                } else {
                    // if there is no user with that username
                    // create the user
                    var pass = await bcrypt.hash(password, 10)
                    var newUserMysql = {
                        email: email,
                        password: pass,
                        fName: req.body.fName,
                        lName: req.body.lName,
                        practice: req.body.practice,
                    };

                    var insertQuery = `INSERT INTO Users(first_name, last_name, role, email, password, practice) VALUE (?, ? ,?, ?, ?, ?)`;
                    connection.query(insertQuery,[newUserMysql.fName,newUserMysql.lName, 0, newUserMysql.email, newUserMysql.password, newUserMysql.practice],function(err, rows) {
                        if(err) console.log(err)

                        newUserMysql.role = 0;
                        console.log(newUserMysql)
                        return done(null, newUserMysql);
                    });
                    
                }
            })   
        })
    );

    // =========================================================================
    // LOCAL LOGIN =============================================================
    // =========================================================================
    // we are using named strategies since we have one for login and one for signup
    // by default, if there was no name, it would just be called 'local'

    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 (!bcrypt.compare(password, rows[0].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]);
            });
            
        })
    );
};

function userExists(data, callback) {
    const sql = `SELECT * FROM Users where email = ?`;
    connection.query(sql, [data], function (err, resp) {
      console.log(resp.length)
      if (resp.length < 1) {
        return callback(false)
      }
      return true;
    })
  }

  function getUser(data, callback) {
    const userSql =`SELECT * FROM Users where userId = ?`;
  
    connection.query(userSql, [data], function (err, resp) {
      if (resp) {
        console.log(resp)
        return callback(resp[0]);
      }
      return null;
    })
  } 
在我的控制台中,我看到这些请求是在哪里发出的:

POST /signup 302 445.782 ms - 60
GET /profile - - ms - -
GET /profile - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -

根据userExists函数,getUser回调似乎不完整

passport.deserializeUser(函数(id,done){
getUser(id,函数(响应){
如果(答复){
完成(空,响应);
}
})        
});
POST /signup 302 445.782 ms - 60
GET /profile - - ms - -
GET /profile - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -
POST /signup - - ms - -