【发布时间】:2018-07-03 16:02:29
【问题描述】:
使用 Passport(Local-Signup) 创建用户注册功能,代码如下:
// config/passport.js
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
// load up the user model
var mysql = require('mysql');
var bcrypt = require('bcrypt-nodejs');
var dbconfig = require('./database');
var connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
// expose this function to our app using module.exports
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
connection.query("SELECT * FROM users WHERE id = ? ",[id], function(err, rows){
done(err, rows[0]);
});
});
passport.use(
'local-signup',
new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, 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
connection.query("SELECT * FROM users WHERE username = ?",[username], function(err, rows) {
if (err)
return done(err);
if (rows.length) {
return done(null, false, req.flash('signupMessage', 'That username is already taken.'));
} else {
// if there is no user with that username
// create the user
var newUserMysql = {
username: username,
password: bcrypt.hashSync(password, null, null) // use the generateHash function in our user model
};
var insertQuery = "INSERT INTO users ( username, password ) values (?,?)";
connection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
});
}
});
})
);
抛出以下错误:
TypeError:无法读取未定义的属性“id” 在 Query._callback (M:\server\config\passport.js:70:55) 在 Query.Sequence.end (M:\server\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24) 在 Query.ErrorPacket (M:\server\node_modules\mysql\lib\protocol\sequences\Query.js:90:8) 在 Protocol._parsePacket (M:\server\node_modules\mysql\lib\protocol\Protocol.js:279:23) 在 Parser.write (M:\server\node_modules\mysql\lib\protocol\Parser.js:76:12) 在 Protocol.write (M:\server\node_modules\mysql\lib\protocol\Protocol.js:39:16) 在套接字。 (M:\server\node_modules\mysql\lib\Connection.js:103:28) 在 emitOne (events.js:116:13) 在 Socket.emit (events.js:211:7) 在 addChunk (_stream_readable.js:263:12) 在 readableAddChunk (_stream_readable.js:250:11) 在 Socket.Readable.push (_stream_readable.js:208:10) 在 TCP.onread (net.js:594:20)
使用console.log(rows); 返回undefined。我该如何解决这个问题?
[编辑]
考虑到答案后,产生了以下错误(因为rows.insertId 仍然未定义):
错误:无法将用户序列化到会话中 通过 (M:\server\node_modules\passport\lib\authenticator.js:281:19) 在序列化 (M:\server\node_modules\passport\lib\authenticator.js:286:7) 在 M:\server\config\passport.js:24:9 通过 (M:\server\node_modules\passport\lib\authenticator.js:294:9) 在 Authenticator.serializeUser (M:\server\node_modules\passport\lib\authenticator.js:299:5) 在 SessionManager.logIn (M:\server\node_modules\passport\lib\sessionmanager.js:14:8) 在 IncomingMessage.req.login.req.logIn (M:\server\node_modules\passport\lib\http\request.js:50:33) 在 Strategy.strategy.success (M:\server\node_modules\passport\lib\middleware\authenticate.js:248:13) 在已验证 (M:\server\node_modules\passport-local\lib\strategy.js:83:10)
【问题讨论】:
-
err是否包含某些内容? -
@El_Matella nope 错误为空。现在重新运行代码产生了另一个错误:
Error: Failed to serialize user into session -
newUserMysql来自哪里? -
@Ravi 它只是 JSON 格式的本地变量。
var newUserMysql = {username: username, password : password}; -
@Valamorde 你能分享一下json吗?
标签: javascript node.js express passport-local