Created
October 27, 2020 07:34
-
-
Save moaoa/9b27d0e3ab853f166d4dc7dc3a250958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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