angularjs - How can I save additional fields upon user signup when using MEAN stack and Passport? -
i'm trying save more email , password when user signs on site. passport examples show email , password being stored. if have "gender", , "name" fields need stored when user signs up? i've spend day trying figure out no luck. here i'm using:
passport.js:
passport.use('local-signup', new localstrategy({ usernamefield : 'email', passwordfield : 'password', passreqtocallback : true }, function(req, email, password, done) { process.nexttick(function() { user.findone({ 'email' : email }, function(err, user) { if (err) return done(err); if (user) { return done(null, false, req.flash('signupmessage', 'email taken.')); } else { var newuser = new user(); newuser.email = email; newuser.password = newuser.generatehash(password); newuser.save(function(err) { if (err) throw err; return done(null, newuser); }); } }); }); }));
routes.js:
app.post('/signup', function(req, res, next) { passport.authenticate('local-signup', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.redirect('/'); } req.login(user, function(err) { if (err) { return next(err); } // load user model var user = require('../app/models/user'); var conditions = { email: req.user.email } , update = { name: req.name, // works if manually set "req.name" "'bill smith'" gender: req.gender // works if manually set "req.gender" "'male'" } , options = { multi: false }; user.update(conditions, update, options, callback); function callback (err, numaffected) { // numaffected number of updated documents } return res.json({ response: 1, username: req.user.email }); }); })(req, res, next); });
angular.js:
$scope.signupmain = function () { var data = $.param({ name: $('#name').val(), email: $('#email').val(), password: $('#password').val(), gender: $('#gender').val() }); $http({ url: 'signup', method: "post", data: data, headers: {'content-type': 'application/x-www-form-urlencoded'} }).success(function (result, status, headers, config) { // }) .error(function (data, status, headers, config) { $scope.status = status; }); };
everything works fine if i'm trying store email , password when users signs up, attempts @ saving "name" , "gender" have failed. basically, in routes.js
unable name: req.name
, gender: req.gender
work. is, req.name
, req.gender
not "getting" values. works if manually enter values such name: 'bill smith'
, gender: 'male'
. doing wrong?
1.- add new attributes passport.js
:
var newuser = new user(); newuser.email = email; newuser.password = newuser.generatehash(password); newuser.gender = req.body.gender; // new attribute (req.body = post data) newuser.save(function(err) { if (err) throw err; return done(null, newuser); });
2.- modify user model , add new attribute schema
Comments
Post a Comment