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

verifyactivesession.js 2.6KB

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