Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

verifyactivesession.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. userService.activeSessions[
  46. hashToMatch
  47. ].emailWasRespondedTo = true
  48. return {
  49. ok: true,
  50. handler: pluginConfig.handlerType,
  51. data: {
  52. hashesMatch: hashToMatch === hash,
  53. },
  54. }
  55. } catch (err) {
  56. console.log('err :=>', err)
  57. return {
  58. ok: false,
  59. handler: pluginConfig.handlerType,
  60. data: {
  61. error: err,
  62. },
  63. }
  64. }
  65. },
  66. validate: {
  67. failAction: 'log',
  68. },
  69. response: {
  70. schema: Joi.object({
  71. ok: Joi.bool(),
  72. handler: Joi.string(),
  73. data: Joi.object(),
  74. }).label('verify_email_res'),
  75. failAction: 'log',
  76. },
  77. },
  78. }