Guest User

Untitled

a guest
Jul 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import bcrypt from 'bcrypt';
  2. import Joi from 'joi';
  3. import createToken from '../utils/createToken';
  4.  
  5. const Models = require('../../models'); // Required DB models
  6.  
  7. export default [{
  8. method: 'POST',
  9. path: '/login',
  10. config: {
  11. auth: false,
  12. validate: {
  13. payload: Joi.object({
  14. userName: Joi.string().min(1).max(10).required(),
  15. password: Joi.string().min(1).required(),
  16. }),
  17. },
  18. },
  19. handler: (req, reply) => {
  20. const { userName, password } = req.payload;
  21. return Models.users.findOne({ where: { user_name: userName } })
  22. .then((userDetails) => {
  23. if (userDetails) {
  24. const isCorrect = bcrypt.compareSync(password, userDetails.password);
  25. if (isCorrect) {
  26. const data = {
  27. token: createToken(userDetails),
  28. };
  29. return reply.response(data).code(200);
  30. }
  31. return reply.response({ message: 'Invalid Username or Password!' }).code(400);
  32. }
  33. return reply.response({ message: 'Invalid Username or Password!' }).code(400);
  34. });
  35. },
  36. }];
Add Comment
Please, Sign In to add comment