Guest User

Untitled

a guest
Jul 30th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import Joi from 'joi';
  2. import hashPassword from '../utils/hashPassword';
  3.  
  4. const Models = require('../../models');
  5.  
  6. export default [{
  7. method: 'POST',
  8. path: '/register',
  9. config: {
  10. auth: false,
  11. validate: {
  12. payload: Joi.object({
  13. firstName: Joi.string().min(1).max(10),
  14. lastName: Joi.string().min(1).max(10),
  15. email: Joi.string().email().required().required(),
  16. userName: Joi.string().min(1).max(10).required(),
  17. password: Joi.string().min(1).required(),
  18. }),
  19. },
  20. },
  21. handler: (req, h) => {
  22. const {
  23. firstName, lastName, email, userName, password,
  24. } = req.payload;
  25. return hashPassword(password).then((passwordHash) => {
  26. const insertIntoDB = {
  27. first_name: firstName,
  28. last_name: lastName,
  29. email,
  30. user_name: userName,
  31. password: passwordHash,
  32. };
  33. return Models.users.findAll({
  34. where: {
  35. $or: [
  36. { email: { $eq: email } },
  37. { user_name: { $eq: userName } },
  38. ],
  39. },
  40. }).then((userExists) => {
  41. if (userExists.length === 0) {
  42. return Models.users.create(insertIntoDB)
  43. .then(() => h.response({ message: 'user successfully registered' }));
  44. }
  45. const responseString = (userExists[0].user_name === userName) ? 'User Name already taken' : 'Email already registered';
  46. return h.response({ message: responseString }).code(400);
  47. });
  48. });
  49. },
  50. }];
Add Comment
Please, Sign In to add comment