Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

send-email.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'email',
  5. docs: {
  6. get: {
  7. description: 'sends confirmation email',
  8. notes: 'Stores the email in memory in a hash and sends confirmation email for signup',
  9. },
  10. },
  11. }
  12. const validators = {
  13. payload: Joi.object({
  14. email: Joi.string(),
  15. name: Joi.string(),
  16. seeking: Joi.string(),
  17. sessionToken: Joi.string(),
  18. }),
  19. }
  20. module.exports = {
  21. method: 'POST',
  22. path: '/send-email/',
  23. options: {
  24. ...pluginConfig.docs.get,
  25. tags: ['api'],
  26. auth: false,
  27. cors: true,
  28. handler: async function (request, h) {
  29. const { userService } = request.server.services()
  30. const userCredentials = request.payload
  31. try {
  32. const emailSent = await userService.emailSent(userCredentials)
  33. const hashedSessionToken = Object.keys(
  34. userService.activeSessions,
  35. ).find(hashedToken => {
  36. return (
  37. userService.activeSessions[`${hashedToken}`].email ===
  38. userCredentials.email
  39. )
  40. })
  41. // Registers the activeSessions object for use by jwt auth strategy
  42. request.server.app.activeSessions = userService.activeSessions
  43. if (!hashedSessionToken?.length) {
  44. throw Error('hashedSessionToken not Found!!')
  45. }
  46. return {
  47. ok: true,
  48. handler: pluginConfig.handlerType,
  49. data: {
  50. emailSentSuccessfully: emailSent.wasSuccessfull,
  51. hashedSessionToken,
  52. },
  53. }
  54. } catch (err) {
  55. console.log('err :=>', err)
  56. return {
  57. ok: false,
  58. handler: pluginConfig.handlerType,
  59. data: {
  60. error: err,
  61. },
  62. }
  63. }
  64. },
  65. validate: {
  66. ...validators,
  67. failAction: 'log',
  68. },
  69. response: {
  70. schema: Joi.object({
  71. ok: Joi.bool(),
  72. handler: Joi.string(),
  73. data: Joi.object(),
  74. }).label('email_res'),
  75. failAction: 'log',
  76. },
  77. },
  78. }