SHARE
TWEET

Untitled

a guest Jan 26th, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.post("/register", upload.single("profile_picture"), (req, res) => {
  2.   User.findOne({ email: req.body.email })
  3.     .then(user => {
  4.       if (user) {
  5.         cloudinary.v2.uploader.destroy(req.file.public_id, (err, result) => console.log(result, err));
  6.         return res.status(400).json({ success: false, message: "User already exists." });
  7.       }
  8.  
  9.       const newUser = new User({
  10.         email: req.body.email,
  11.         password: req.body.password,
  12.         name: req.body.name,
  13.         profile_picture: {
  14.           url: "",
  15.           public_id: ""
  16.         }
  17.       });
  18.  
  19.       bcrypt.genSalt(10, (err, salt) => {
  20.         if (err) {
  21.           cloudinary.v2.uploader.destroy(req.file.public_id, (err, result) => console.log(result, err));
  22.           return res.status(500).json({ success: false, message: "User could not be created." });
  23.         }
  24.  
  25.         bcrypt.hash(newUser.password, salt, (err, hash) => {
  26.           if (err) {
  27.             cloudinary.v2.uploader.destroy(req.file.public_id, (err, result) => console.log(result, err));
  28.             return res.status(500).json({ success: false, message: "User could not be created." });
  29.           }
  30.  
  31.           newUser.password = hash;
  32.           newUser.profile_picture.url = req.file.url;
  33.           newUser.profile_picture.public_id = req.file.public_id;
  34.  
  35.           newUser.save();
  36.  
  37.           return res.status(200).json({ success: true, message: "User successfully registered." });
  38.         });
  39.       })
  40.     })
  41.     .catch(err => {
  42.       console.log(err);
  43.       return res.status(500).json({ success: false, message: "User could not be created." });
  44.     });
  45. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top