'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'email', docs: { get: { description: 'verifies confirmation email', notes: 'Verifies the email from the stored hash', }, }, } const validators = { params: Joi.object({ hashedSessionToken: Joi.string(), }), } module.exports = { method: 'GET', path: '/verify/{hashedSessionToken}', options: { ...pluginConfig.docs.get, tags: ['api'], auth: false, cors: true, handler: async function (request, h) { const { userService } = request.server.services() const hash = request.params.hashedSessionToken try { const hashToMatch = Object.keys( userService.activeSessions, ).find(hashedToken => { return hashedToken === hash }) if (!hashToMatch?.length) { throw Error('[API] hashToMatch Not Found!') } const now = Date.now() const expiration = new Date( userService.activeSessions[`${hash}`].expiration, ) if (now > expiration) { delete userService.activeSessions[hashToMatch] throw new Error( '[API] you took to long to respond to the email...', ) } if (!hashToMatch) { throw new Error('[API] no record of email in cache') } // NOTE: When user responds to email, // boolean value is set to true, allowing user back into the survey userService.activeSessions[ hashToMatch ].emailWasRespondedTo = true return { ok: true, handler: pluginConfig.handlerType, data: { hashesMatch: hashToMatch === hash, }, } } catch (err) { return { ok: false, handler: pluginConfig.handlerType, data: { error: err.message, }, } } }, validate: { ...validators, failAction: 'log', }, response: { schema: Joi.object({ ok: Joi.bool(), handler: Joi.string(), data: Joi.object(), }).label('verify_email_res'), failAction: 'log', }, }, }