You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verifyactivesession.js 2.3KB

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