Created
June 22, 2019 19:36
-
-
Save chris001177/03e4a57da784909a8efaea5f9871ccee 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
const bcrypt = require('bcrypt') | |
const jsonwebtoken = require('jsonwebtoken') | |
app.post('/login', (req, res) => { | |
const user = await User.findOne({ where: { req.body.email } }) | |
if (!user) { | |
throw new Error('No user with that email') | |
} | |
const valid = await bcrypt.compare(req.body.password, user.password) | |
if (!valid) { | |
throw new Error('Incorrect password') | |
} | |
// signin user and generate a jwt | |
const token = jsonwebtoken.sign({ | |
id: user.id, | |
email: user.email | |
}, 'somesuperdupersecret', { expiresIn: '1y' }) | |
// return json web token | |
res.json({ | |
message: 'Authentication successful!', | |
data: token | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment