| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- '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/{hashedEmail}',
- options: {
- ...pluginConfig.docs.get,
- tags: ['api'],
- auth: false,
- cors: true,
- handler: async function (request, h) {
- const { userService } = request.server.services()
- const hash = request.params.hashedEmail
-
- try {
- const hashToMatch = Object.keys(userService.hashedEmails).find(
- email => {
- return email === hash
- },
- )
- const now = Date.now()
- // TODO: convert this back to a date object
- const expiration = userService.hashedEmails[hashToMatch]
- if (now > expiration) {
- delete userService.hashedEmails[hashToMatch]
- throw new Error(
- 'you took to long to respond to the email...',
- )
- }
- if (!hashToMatch) {
- throw new Error('no record of email in cache')
- }
-
- 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',
- },
- },
- }
|