Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require("express");
- const router = express.Router();
- const User = require("../../models/User");
- const gravatar = require("gravatar");
- const bcrypt = require("bcryptjs");
- const validateLoginInput = require("../../validation/login");
- router.post("/register",(req,res) =>{
- const {errors,isValid} = validateRegisterInput(req.body);
- if(!isValid){
- return res.status(400).json(errors);
- }
- User.findOne({email: req.body.email})
- .then(user => {
- if(user){
- return res.status(400).json({'email' : 'Alamat email sudah digunakan'});
- }else{
- const newUser = new User({
- name : req.body.name,
- email : req.body.email,
- password : req.body.password
- });
- bcrypt.genSalt(10,(err,salt) => {
- bcrypt.hash(newUser.password,salt,(err,hash) => {
- if(err) throw err;
- newUser.password = hash;
- newUser.save()
- .then(user => res.json(user))
- .catch(err => console.log(err))
- return res.json(newUser);
- })
- });
- }
- })
- });
- module.exports = router;
Add Comment
Please, Sign In to add comment