Advertisement
Guest User

Untitled

a guest
Jan 10th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.post('/users/login', function(req, res, next){
  2.   if(!req.body.user.email){
  3.     return res.status(422).json({errors: {email: "can't be blank"}});
  4.   }
  5.  
  6.   if(!req.body.user.password){
  7.     return res.status(422).json({errors: {password: "can't be blank"}});
  8.   }
  9.  
  10.   passport.authenticate('local', {session: false}, function(err, user, info){
  11.     if(err){ return next(err); }
  12.  
  13.     if(user){
  14.       user.token = user.generateJWT();
  15.       return res.json({user: user.toAuthJSON()});
  16.     } else {
  17.       return res.status(422).json(info);
  18.     }
  19.   })(req, res, next);
  20. });
  21.  
  22. router.post('/users', function(req, res, next){
  23.   var user = new User();
  24.  
  25.   user.username = req.body.user.username;
  26.   user.email = req.body.user.email;
  27.   user.setPassword(req.body.user.password);
  28.  
  29.   user.save().then(function(){
  30.     return res.json({user: user.toAuthJSON()});
  31.   }).catch(next);
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement