我使用passport.js获取为Node + Express + Sequelize应用程序建立的基本注册功能。现在我有一个空的数据库,我只使用护照本地策略从表单中获取用户的电子邮件和密码,检查数据库以确保电子邮件不与其他帐户关联,然后一旦建立,创建用户。
以下是我的代码的相关部分:
const db = require('./sequelize');
const passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(user, done) {
db.User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local', new LocalStrategy({
emailField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(email, password, done){
process.nextTick(function() {
// 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
db.User.findOne({ email : email }, function(err, user) {
// if there are any errors, return the error
if (err)
return done(null, false, {message: err});
// check to see if there's already a user with that email
else if (user) {
return done(null, false, {message: 'User already exists'});
}
else {
return done(null, true, {message: 'User created'});
}
});
});
}
));
module.exports = passport;
app.post('/signup', function(req, res, next){
const user = req.body;
passport.authenticate('local', function(err, user, info){
if (err){
res.send({success: false, message: 'authentication failed'});
}
if (!user) {
console.log(user);
res.send({message: "user already found"});
}
else{
db.User.create({
email: req.body.username,
password: req.body.password
});
console.log(info);
res.json({message: "user successfully signed up"});
}
})(req, res, next)
});
const path = require('path');
const Sequelize = require('sequelize');
const _ = require('lodash');
const config = require('./config');
const db = {};
// create your instance of sequelize
const sequelize = new Sequelize(config.db.name, config.db.username, config.db.password, {
host: config.db.host,
port: config.db.port,
dialect: 'mysql',
storage: config.db.storage
});
//Instantiate the database
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
}, function (err) {
console.log('Unable to connect to the database:', err);
});
/* this is where all of the MODELS will be defined for the time being */
const User = sequelize.define('User', {
username: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING
});
sequelize.sync();
db.User = User;
db.Sequelize = Sequelize;
db.sequelize = sequelize;
module.exports = db;
然而,当我使用干净的数据库运行应用程序时,app.js文件中的用户评估为false,我无法注册新用户,因为它似乎是策略认为已经存在数据库中包含该电子邮件的现有用户。我需要做些什么才能解决问题?
答案 0 :(得分:0)
db.User.findOne({ where: { email : email }})
.then(
function(user) {
if (user) {
return done(null, false, {message: 'User already exists'});
} else {
return done(null, true, {message: 'User created'});
);
答案 1 :(得分:0)
尝试更改代码
在此处使用重定向( app.js ):
app.post('/signup', passport.authenticate('local', {
successRedirect : '/profile',
failureRedirect : '/signup',
failureFlash : true
}));
使用续集查询的承诺( ./ config / passport.js ):
passport.deserializeUser(function(id, done) {
db.User.findById(id).then(function(user) {
done(err, user.dataValues);
})
.catch(function(err){
done(err, null);
});
});
passport.use('local', new LocalStrategy({
emailField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done){
process.nextTick(function() {
// 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
db.User.findOne({ where: {email : email }})
.then(function(user) {
if (user !== null) {
done(null, false, {message: 'User already exists'});
}
else {
db.User.create({
email: email,
password: password
})
.then(function(user){
done(null, user);
})
.catch(function(err){
console.log(err);
done(null, false);
});
}
})
.catch(function(err){
// if there are any errors, return the error
return done(null, false, {message: err});
});
});
}
));