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

verify-session.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'email',
  5. docs: {
  6. get: {
  7. description: 'verifies confirmation email',
  8. notes: 'Verifies the email from the stored hash',
  9. },
  10. },
  11. }
  12. const validators = {
  13. params: Joi.object({
  14. hashedSessionToken: Joi.string(),
  15. }),
  16. }
  17. module.exports = {
  18. method: 'GET',
  19. path: '/verify/{hashedSessionToken}',
  20. options: {
  21. ...pluginConfig.docs.get,
  22. tags: ['api'],
  23. auth: false,
  24. cors: true,
  25. handler: async function (request, h) {
  26. const { userService } = request.server.services()
  27. const hash = request.params.hashedSessionToken
  28. try {
  29. const hashToMatch = Object.keys(
  30. userService.activeSessions,
  31. ).find(hashedToken => {
  32. return hashedToken === hash
  33. })
  34. if (!hashToMatch?.length) {
  35. throw Error('hashToMatch Not Found!')
  36. }
  37. const now = Date.now()
  38. const expiration = new Date(
  39. userService.activeSessions[`${hash}`].expiration,
  40. )
  41. if (now > expiration) {
  42. delete userService.activeSessions[hashToMatch]
  43. throw new Error(
  44. 'you took to long to respond to the email...',
  45. )
  46. }
  47. if (!hashToMatch) {
  48. throw new Error('no record of email in cache')
  49. }
  50. // NOTE: When user responds to email,
  51. // boolean value is set to true, allowing user back into the survey
  52. userService.activeSessions[hashToMatch].emailWasRespondedTo =
  53. true
  54. return {
  55. ok: true,
  56. handler: pluginConfig.handlerType,
  57. data: {
  58. hashesMatch: hashToMatch === hash,
  59. },
  60. }
  61. } catch (err) {
  62. return {
  63. ok: false,
  64. handler: pluginConfig.handlerType,
  65. data: {
  66. error: err.message,
  67. },
  68. }
  69. }
  70. },
  71. validate: {
  72. ...validators,
  73. failAction: 'log',
  74. },
  75. response: {
  76. schema: Joi.object({
  77. ok: Joi.bool(),
  78. handler: Joi.string(),
  79. data: Joi.object(),
  80. }).label('verify_email_res'),
  81. failAction: 'log',
  82. },
  83. },
  84. }