Guest User

Untitled

a guest
Aug 6th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. const express = require("express");
  2. const router = express.Router();
  3. const User = require("../../models/User");
  4. const gravatar = require("gravatar");
  5. const bcrypt = require("bcryptjs");
  6. const validateLoginInput = require("../../validation/login");
  7.  
  8.  
  9. router.post("/register",(req,res) =>{
  10. const {errors,isValid} = validateRegisterInput(req.body);
  11.  
  12. if(!isValid){
  13. return res.status(400).json(errors);
  14. }
  15.  
  16. User.findOne({email: req.body.email})
  17. .then(user => {
  18. if(user){
  19. return res.status(400).json({'email' : 'Alamat email sudah digunakan'});
  20. }else{
  21. const newUser = new User({
  22. name : req.body.name,
  23. email : req.body.email,
  24. password : req.body.password
  25. });
  26. bcrypt.genSalt(10,(err,salt) => {
  27. bcrypt.hash(newUser.password,salt,(err,hash) => {
  28. if(err) throw err;
  29. newUser.password = hash;
  30. newUser.save()
  31. .then(user => res.json(user))
  32. .catch(err => console.log(err))
  33. return res.json(newUser);
  34. })
  35. });
  36. }
  37. })
  38. });
  39.  
  40. module.exports = router;
Add Comment
Please, Sign In to add comment