Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. login: {
  2. auth: false,
  3. description: 'User.Login',
  4. notes: 'Verify credentials, provide a JWT token and returns the user information',
  5. tags: ['api'],
  6. validate: {
  7. payload: Joi.object({
  8. email: Joi.string().email().required(),
  9. password: Joi.string().min(6).max(60).required()
  10. })
  11. },
  12. handler: (request, reply) => {
  13. const { email, password } = request.payload
  14. const key = plugin.plugins['system'].privateKey
  15. const expires = moment().add(7, 'days').valueOf()
  16. const omit = ['_id', '__v', 'password', 'status', 'hashes', 'scope']
  17.  
  18. User.findOneAsync({ email: email, status: { $in: [ User.status.ACTIVE ] } }).bind({})
  19. .then(user => {
  20. if (!user) throw Boom.unauthorized('Bad username or password')
  21.  
  22. this.user = user
  23.  
  24. return bcrypt.compareAsync(password, user.password)
  25. })
  26. .then(valid => {
  27. if (!valid) throw Boom.unauthorized('Bad username or password')
  28.  
  29. return User.login(this.user._id)
  30. })
  31. .then(user => {
  32. this.user = user
  33.  
  34. return jwt.signAsync({ _id: this.user._id, exp: expires }, key, { algorithm: 'HS256' })
  35. })
  36. .then(token => {
  37. return _.omit(_.merge(this.user._doc, { token: token }), omit)
  38. })
  39. .catch(Calibrate.error)
  40. .then(reply)
  41. }
  42. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement