Nodejs + Passport:如何添加更多的用户信息





Solutions Collecting From Web of "Nodejs + Passport:如何添加更多的用户信息"

在您的Passport.js注册/注册策略中,您应该能够将请求对象作为该函数的第一个parameter passing,Passport将负责将您的请求传递给您的函数。



 passport.use('signup', new LocalStrategy({ passReqToCallback : true }, function(req, username, password, done) { findOrCreateUser = function(){ // find a user in Mongo with provided username User.findOne({'username':username},function(err, user) { // In case of any error return if (err){ console.log('Error in Signup: ' + err); return done(err); } // already exists if (user) { console.log('User already exists'); return done(null, false, req.flash('message','User Already Exists')); } else { // if there is no user with that email // create the user var newUser = new User(); // set the user's local credentials newUser.username = username; newUser.password = createHash(password); newUser.firstName = req.body.firstName; newUser.lastName = req.body.lastName; // save the user { if (err){ console.log('Error in Saving user: '+err); throw err; } console.log('User Registration succesful'); return done(null, newUser); }); } }); }; // Delay the execution of findOrCreateUser and execute // the method in the next tick of the event loop process.nextTick(findOrCreateUser); }); ); 

这里有一个教程 ,更详细地介绍它。 我没有将params中的firstName和lastName参数更改为正文中的variables。 但是,您可以使用params或body来将这些数据导入到本地策略中。

我发现这个效果更好。 在req,username,password之后,但是在你想要传递给函数的其他variables之前,这似乎是关键。 如果完成了,那么:

 events.js:160 throw er; // Unhandled 'error' event TypeError: done is not a function 


 // ========================================================================= // 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 : 'username', passwordField : 'password', passReqToCallback : true // allows us to pass back the entire request to the callback }, function(req, username, password, done, fname, lname, email, security_question, security_answer) { // asynchronous // User.findOne wont fire unless data is sent back 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 User.findOne({ 'local.username' : username }, function(err, user) { // if there are any errors, return the error if (err) return done(err); // check to see if theres already a user with that email if (user) { return done(null, false, req.flash('signupMessage', 'That username is already taken.')); } else { // if there is no user with that email // create the user var newUser = new User(); // set the user's local credentials newUser.local.fname = fname; newUser.local.lname = lname; newUser.local.username = username; = email; newUser.local.password = newUser.generateHash(password); newUser.local.security_question = security_question; newUser.local.security_answer = newUser.generateHash(security_answer); // save the user { if (err) throw err; return done(null, newUser); }); } }); }); }));