Skip to content

Instantly share code, notes, and snippets.

@chris001177
Created June 22, 2019 19:36
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 chris001177/03e4a57da784909a8efaea5f9871ccee to your computer and use it in GitHub Desktop.
Save chris001177/03e4a57da784909a8efaea5f9871ccee to your computer and use it in GitHub Desktop.
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