因此,尝试为用户注册创建本地策略,非常简单的前端,四个字段;用户名,电子邮件,密码(然后再次输入密码,因为它在表单中)。我知道帖子有效,以下信息作为其中的一部分发送
username:user
email:user@test.com
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/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);
});
}
});
})
);
};
问题出在哪里,或最容易调试的方法?
答案 0 :(得分:1)
最后问题很简单,与护照无关。
我错过了解码传入的数据。 添加以下内容并劫掠你的叔叔(发送一张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
}));