| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 'use strict'
-
- const Joi = require('joi')
-
- const pluginConfig = {
- handlerType: 'email',
- docs: {
- get: {
- description: 'verifies confirmation email',
- notes: 'Verifies the email from the stored hash',
- },
- },
- }
-
- 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
- })
- console.log('hashToMatch :=>', hashToMatch)
- if (!hashToMatch?.length) {
- throw Error('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(
- 'you took to long to respond to the email...',
- )
- }
- if (!hashToMatch) {
- throw new Error('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) {
- console.log('err :=>', err)
- return {
- ok: false,
- handler: pluginConfig.handlerType,
- data: {
- error: err,
- },
- }
- }
- },
- validate: {
- failAction: 'log',
- },
- response: {
- schema: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: Joi.object(),
- }).label('verify_email_res'),
- failAction: 'log',
- },
- },
- }
|