我正在使用Passport.js与React.js和MySQL一起实现用户注册和登录系统。
但是,当我点击获取下面的api路由的注册按钮时,我收到一个错误消息。错误为错误:未知身份验证策略“本地注册”
app.post('/api/signup', passport.authenticate('local-signup'));
下面是我的脚本,问题是当我尝试注册时
var passport = require('passport');
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
// 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) {
con.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
//nameField : 'usernane',
usernameField : 'username',
passwordField : 'password',
//locationField : 'location',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, username, password, done) {
console.log("This is twas");
// 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
con.query("select * from users where username = '"+username+"'",function(err,rows){
console.log(rows);
console.log("above row object");
if (err)
return done(err);
if (rows.length) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
// if there is no user with that email
// create the user
var newUserMysql = new Object();
// newUserMysql.username = username;
newUserMysql.username = username;
newUserMysql.password = password; // use the generateHash function in our user model
var insertQuery = "INSERT INTO users (username, password) values (" + username +"','"+ password +"')";
console.log(insertQuery);
con.query(insertQuery,function(err,rows){
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
});
}
});
}));
};
The route below is fetched once the user clicks the register button.
app.post('/ api / signup',护照.authenticate('local-signup'));
答案 0 :(得分:1)
当前创建函数的方式有问题。
当前在要导出的函数之外声明了 passport
,并且您正在使用与该函数所需要的参数相同的passport
变量。这就是为什么您会收到未知的策略错误的原因。
您可以将外部声明移至导出的函数中,删除参数,然后在不带任何参数的情况下调用函数。
module.exports = function () {
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
// the rest of your passport configuration goes here...
// return passport;
};
或者您可以通过这种方式完成
module.exports = function (passport) {
var LocalStrategy = require('passport-local').Strategy;
// the rest of your passport configuration goes here...
// return passport;
};
然后在您需要配置的文件中,执行此操作
var passport = require('passport');
// ...
var config = require('/path/to/passport-function');
// then call config with passport
var passport_config = config(passport);
// you would then have to use passport_config if you want to use the newly-created strategy
我希望这会有所帮助。