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

verifyemail.js 2.4KB

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/{hashedEmail}',
  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.hashedEmail
  23. try {
  24. // Is there an userService.activeSession?
  25. // If there is, issue a new access token
  26. // We need the session token from the frontend
  27. // If NOT, throw error (kicks back to login)
  28. const hashToMatch = Object.keys(userService.hashedEmails).find(
  29. email => {
  30. return email === hash
  31. },
  32. )
  33. const now = Date.now()
  34. // TODO: convert this back to a date object
  35. const expiration = userService.hashedEmails[hashToMatch]
  36. if (now > expiration) {
  37. delete userService.hashedEmails[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: { hashesMatch: hashToMatch === hash },
  49. }
  50. } catch (err) {
  51. console.log('err :=>', err)
  52. return {
  53. ok: false,
  54. handler: pluginConfig.handlerType,
  55. data: {
  56. error: err,
  57. },
  58. }
  59. }
  60. },
  61. validate: {
  62. failAction: 'log',
  63. },
  64. response: {
  65. schema: Joi.object({
  66. ok: Joi.bool(),
  67. handler: Joi.string(),
  68. data: Joi.object(),
  69. }).label('verify_email_res'),
  70. failAction: 'log',
  71. },
  72. },
  73. }