'use strict' const Joi = require('joi') const pluginConfig = { handlerType: 'email', docs: { get: { description: 'sends confirmation email', notes: 'Stores the email in memory in a hash and sends confirmation email for signup', }, }, } module.exports = { method: 'POST', path: '/send-email/', options: { ...pluginConfig.docs.get, tags: ['api'], auth: false, cors: true, handler: async function (request, h) { const { userService } = request.server.services() const userCredentials = request.payload try { const emailSent = await userService.emailSent(userCredentials) const hashedSessionToken = Object.keys( userService.activeSessions, ).find(hashedToken => { return ( userService.activeSessions[`${hashedToken}`].email === userCredentials.email ) }) // Registers the activeSessions object for use by jwt auth strategy request.server.app.activeSessions = userService.activeSessions if (!hashedSessionToken?.length) { throw Error('hashedSessionToken not Found!!') } return { ok: true, handler: pluginConfig.handlerType, data: { emailSentSuccessfully: emailSent.wasSuccessfull, hashedSessionToken, }, } } 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('email_res'), failAction: 'log', }, }, }