Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // het kijkt naar user -> wachtwoord controleren en maakt token aan
  2. const Joi = require(`joi`);
  3. const {User} = require(`mongoose`).models;
  4. const Boom = require(`boom`);
  5.  
  6. module.exports = [
  7. {
  8. method: `POST`,
  9. path: `/api/auth`,
  10. config: {
  11. validate: {
  12. options: {
  13. abortEarly: false
  14. },
  15. payload: {
  16. login: Joi.string().min(3).required(),
  17. password: Joi.string().min(3).required(),
  18. audience: Joi.string().min(3).required()
  19. }
  20. }
  21. },
  22. handler: (req, res) => {
  23. const {login, password, audience} = req.payload;
  24. const isActive = true;
  25.  
  26. User.findOne({
  27. $and: [
  28. {
  29. $or: [
  30. {username: login},
  31. {email: login}
  32. ]
  33. },
  34. {isActive}
  35. ]
  36. }).then(user => {
  37. if(!user) return(res(Boom.badRequest(`user/password combination incorrect`)));
  38. user.verifyPassword(password, (err, isValid) => {
  39. if(err || !isValid){
  40. return res(
  41. Boom.badRequest(`user/password combination incorrect`)
  42. );
  43. }
  44. return res({token: `nfazlejkfbazej`});
  45. });
  46. }).catch(() => {
  47. return res(
  48. Boom.badRequest(`error while authenticating user`)
  49. );
  50. });
  51. }
  52. }
  53.  
  54.  
  55. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement