用户注册使用护照+ express + mysql + sequelize

我正在尝试使用Passport,MySql,NodeJS和Sequelize构build用户注册api。 我面对的唯一问题是,当一个用户注册一次,他试图用相同的电子邮件重新注册用户被抛出一个401未经授权的错误,而不是用户对象。 当我试图debugging相同的响应,我从服务器得到这个[对象SequelizeInstance:用户]。 这些文件已经在下面提到。 多谢提前一吨!!!

Passport.js文件:

var LocalStrategy = require('passport-local').Strategy; var mysql = require('mysql'); var Model = require('../models/models.js'); // 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.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]); }); }); // ========================================================================= // 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: 'password', passReqToCallback: true // allows us to pass back the entire request to the callback }, function(req, email, password, done) { Model.User.findOne({ where: { email: email } }).then(function(user) { if (user == null) { Model.User.create({ email: email, password: password }).then(function(user) { return done(null, user); }).catch(function(err) { return done(null, err); }); } else { return done(null, false); } }) })); 

};

注册api:

 router.post('/signup', passport.authenticate('local-signup'), function(req, res) { // If this function gets called, authentication was successful. // `req.user` contains the authenticated user. console.log(req.user); if(req.user){ res.send({ success: true, response: 'signup successful' }); } else { res.send({ success: false, response: 'Email already in use' }); } }); 

用户模型是:

 //models/users.js var Sequelize = require('sequelize') var attributes = { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, name: { type: Sequelize.STRING }, email: { type: Sequelize.STRING }, password: { type: Sequelize.STRING }, created_by: { type: Sequelize.INTEGER } } var options = { // Add the timestamps attributes (updatedAt, createdAt) timestamps: true, // don't delete database entries but set the newly added attribute deletedAt // to the current date (when deletion was done). paranoid will only work if // timestamps are enabled paranoid: true, // don't use camelcase for automatically added attributes but underscore style // so updatedAt will be updated_at underscored: true, // disable the modification of table names; By default, sequelize will automatically // transform all passed model names (first parameter of define) into plural. // if you don't want that, set the following freezeTableName: true, // define the table's name tableName: 'users' } module.exports.attributes = attributes module.exports.options = options 

自动表格创build模型脚本是:

 // models/models.js var UserMeta = require('./users.js'), connection = require('./index.js') var User = connection.define('users', UserMeta.attributes, UserMeta.options) // force: true will drop the table if it already exists User.sync({ force: true, match: /_servernew$/ }).then(function() { // Table created return User.create(); }); // you can define relationships here module.exports.User = User; 

所以我想出了解决scheme。 以下代码需要更改。

 router.post('/signup', function(req, res, next) { passport.authenticate('local-signup', function(err, user, info) { if(user){ req.logIn(user, function(err) { if (err) { return next(err); } else { res.send({ success: true, response: 'signup successful' }); } }); } if(!user){ res.send({ success: false, response: 'Authentication Failed' }); } if(err){ res.send({ success: false, response: 'Authentication failed' }) } })(req, res, next); }); 

和passport.js代码应该是这样的。

  // ========================================================================= // 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: 'password', passReqToCallback: true // allows us to pass back the entire request to the callback }, function(req, email, password, done) { Model.User.findOne({ where: { email: email } }).then(function(user, err) { console.log('I entered'+user); console.log('I entered'+err); if(err) { console.log(err); return done(null, false); } if(user == null) { Model.User.create({ email: email, password: password }).then(function(user) { return done(null, user); }).catch(function(err) { return done(null, err); }); } if(user){ return done(null, false); } }) })); 

它会像魅力一样工作:D。