import withSession from 'lib/withSession' import dbConnect from 'lib/dbConnect' import sendMail from 'lib/sendMail' import User from 'models/User' import {subject, text, html} from 'helpers/email' export default withSession(async (req, res) => { await dbConnect() switch (req.method) { case 'GET': try { const {email} = req.session.get('user') if (!email) { throw new Error('Something went wrong') } const key = await User.getVerificationKey(email) if (!key) { throw new Error('Something went wrong') } const response = await sendMail(email, subject, text(key), html(key)) if (!response?.accepted?.length) { throw new Error('Something went wrong') } res.status(204).send() } catch (error) { res.status(400).send() } break case 'POST': try { if (req.body.key) { const user = await User.verifyUser(req.body._id, req.body.key) req.session.set('user', user) await req.session.save() res.status(200).json(user) } } catch (error) { console.log(error) res.status(400).send() } break default: res.status(400).send() break } })