Skip to content

Instantly share code, notes, and snippets.

@deepakkhattar26o2
Created January 12, 2022 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakkhattar26o2/30110c0df821b5e25db8c62275d56af6 to your computer and use it in GitHub Desktop.
Save deepakkhattar26o2/30110c0df821b5e25db8c62275d56af6 to your computer and use it in GitHub Desktop.
A Gist of Sign-Up request handler demonstrating encrypting a password
const express = require('express')
const router = express.Router()
const bcrypt =require('bcrypt')
const User = require('../models/User')
router.post('/signup', (req, res, next)=>{
const emailId = req.body.emailId
User.findOne({emailId: emailId}).exec().then(
user=>{
if(user){
res.status(409).json({
Message: 'An Account With Same Email Id Exists'
})
}
else{
const password = req.body.password
bcrypt.hash(password, 10, (err, hash)=>{
if(err){
return res.status(500).json(err)
}
const newUser = new User({
emailId: emailId,
password: hash
})
newUser.save().then(
doc=>{
res.status(200).json(doc)
}
).catch(
err=>{
res.status(500).json(err)
}
)
})
}
}
)
.catch(
err=>{res.status(500).json(err)}
)
})
module.exports = router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment