Skip to content

Instantly share code, notes, and snippets.

@moaoa
Created October 27, 2020 07:34
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 moaoa/9b27d0e3ab853f166d4dc7dc3a250958 to your computer and use it in GitHub Desktop.
Save moaoa/9b27d0e3ab853f166d4dc7dc3a250958 to your computer and use it in GitHub Desktop.
// sign up route
Router.post('/signUp',cors(), async (req, res) => {
const {email, password, name, imgUrl} = req.body
if(!email || !password || !name) return res.status(400).json({msg: 'all fields are required'})
try {
const user = await User.findOne({email})
if(user) return res.status(400).json({msg: 'email already exists'})
const hashedPassword = await bcrypt.hash(password + '', 10)
const newUser = new User({email, password: hashedPassword, name, imgUrl})
await newUser.save()
res.status(201).send()
} catch (error) {
res.status(500).send()
console.log(error);
}
})
// sign in route
Router.post('/signIn', cors(), async (req, res)=> {
console.log('here');
const {email, password} = req.body
if(!email || !password) return res.status(400).json({msg: 'all fields are required'})
try {
const user = await User.findOne({email})
if(!user) return res.status(401).json({msg: 'email or password is wrong'})
const match = await bcrypt.compare(password + '', user.password)
if(match){
const token = generateToken({_id: user._id})
res.json({token})
}else{
res.status(401).json({msg: 'email or password is wrong'})
}
} catch (error) {
console.log(error);
res.status(500).send()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment