Advertisement
Guest User

Untitled

a guest
Sep 5th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.post('/register', (req, res) => {
  2.   const {errors, isValid} = validateRegisterInput(req.body);
  3.   if(!isValid) {
  4.     return res.status(400).json(errors);
  5.   }
  6.  
  7.   User.findOne({email: req.body.email})
  8.   .then(user => {
  9.     if(user) {
  10.       errors.email = 'Email already exists';
  11.       return res.status(400).json(errors);
  12.     }  
  13.      
  14.       const newUser = new User({
  15.         name: req.body.name,
  16.         email: req.body.email,
  17.         avatar,
  18.         password: req.body.password
  19.       })
  20.      
  21.       bcrypt.genSalt(10, (err, salt) => {
  22.         bcrypt.hash(newUser.password, salt, (err, hash) => {
  23.           if(err) throw err;
  24.           newUser.password = hash;
  25.           newUser.save()
  26.           .then(user => res.json(user))
  27.           .catch(err => console.log(err))
  28.         })
  29.       })
  30.     }
  31.   })
  32. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement