Im wondering if it's okay to create a token while posting a new user as below in the success block as i don't want a user to create an account and also then need to authenticate // create a new user account router.post('/user', function(req, res) { if (!req.body.username || !req.body.password || !req.body.email) { res.json({success: false, msg: 'Please enter a userame, password and an email.'}); } else { var newUser = new User({ username: req.body.username, password: req.body.password, email: req.body.email }); // save the user newUser.save(function(err) { if (err) { return res.json({success: false, msg: 'Username/email already exists.'}); } var token = jwt.encode(newUser,config.jwt_secret); res.json({success: true, msg: 'Successful created new user.',token: 'JWT ' + token ,user: newUser }); }); } });
Submitted January 02, 2017 at 12:57AM by farhansyed7911
No comments:
Post a Comment