因此,尝试为用户创建本地策略,以非常简单的前端注册四个字段;用户名,电子邮件,密码(然后再输入一次密码,因为其形式)。我知道该帖子有效,以下信息是其中的一部分
username:user
email:[email protected]
password:IAMPASSWORD
passwordConfirm:IAMPASSWORD
然后,我使用我所理解的非常简单的passport.authenticate(好,我认为应该很简单,但显然不是那么简单)。
var cfgWebPage = require('../config/webpage.js')
module.exports = function(app, passport) {
/* GET home page. */
app.get('/', function (req, res, next) {
res.render('index', {title: 'Express'});
});
//This is signup
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('signup.ejs', { title: 'Sign up to our service' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('signupMessage') });
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
}
它肯定走得太远了(更改为console.log消息只是为了测试)。
我从服务器返回响应,它执行302,并且其行为好像失败了,但控制台中没有任何响应。
// required for passport
app.use(session({secret: 'Thisisnottherealone',
saveUninitialized: true,
resave: true
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
//Configuring the passports
require('./config/passport')(passport);
var routes = require('./routes/index')(app, passport);
因此,我猜它找到了护照-希望它能
否则抱怨。
似乎要求进行身份验证-那里没有错误
我曾尝试更改passport.js文件中的内容(包含我的护照设置以进行注册),但似乎从未实现,似乎在打开数据库后的某个时间就死了。
这是passport.js文件(来自config)
// config/passport.js
// load up the user model
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 sqlConnection = mysql.createConnection(dbconfig.sqlConnection);
sqlConnection.query('USE ' + dbconfig.sqlDatabase);
// expose this function to our app using module.exports
module.exports = function(passport, app) {
// =========================================================================
// 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) {
// User.findById(id, function(err, user) {
// done(err, user);
// });
passport.deserializeUser(function(id, done) {
console.log('deserialising');
sqlConnection.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
emailField : 'email',
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, 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
console.log("Calling database!");
sqlConnection.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,
email: email,
password: bcrypt.hashSync(password, null, null) // use the generateHash function in our user model
};
var insertQuery = "INSERT INTO users ( username, email, password ) values (?,?)";
sqlConnection.query(insertQuery,[newUserMysql.username, newUserMysql.password],function(err, rows) {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
});
}
});
})
);
};
有什么建议可以解决问题,或者是最简单的调试方法?
最佳答案
最后,问题很简单,与护照无关。
我错过了解码传入的数据。
添加以下内容并让您的叔叔鲍勃(发送没有任何用户信息的passport.authenticate会使护照难过,并使其全部死于无声)。
var bodyParser = require('body-parser');
module.exports = function(app, passport) {
var urlencodedParser = bodyParser.urlencoded({extended: false})
/* GET home page. */
==== API调用示例=====
app.post('/login', urlencodedParser, passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));