Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

verify-session.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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('[API] 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. '[API] you took to long to respond to the email...',
  45. )
  46. }
  47. if (!hashToMatch) {
  48. throw new Error('[API] 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[
  53. hashToMatch
  54. ].emailWasRespondedTo = true
  55. return {
  56. ok: true,
  57. handler: pluginConfig.handlerType,
  58. data: {
  59. hashesMatch: hashToMatch === hash,
  60. },
  61. }
  62. } catch (err) {
  63. return {
  64. ok: false,
  65. handler: pluginConfig.handlerType,
  66. data: {
  67. error: err.message,
  68. },
  69. }
  70. }
  71. },
  72. validate: {
  73. ...validators,
  74. failAction: 'log',
  75. },
  76. response: {
  77. schema: Joi.object({
  78. ok: Joi.bool(),
  79. handler: Joi.string(),
  80. data: Joi.object(),
  81. }).label('verify_email_res'),
  82. failAction: 'log',
  83. },
  84. },
  85. }