import dbConnect from 'lib/dbConnect' import withSession from 'lib/withSession' import sendMail from 'lib/sendMail' import {subject, text, html} from 'helpers/email' import User from 'models/User' import NoteList from 'models/NoteList' export default withSession(async (req, res) => { await dbConnect() switch (req.method) { case 'POST': try { const noteList = await NoteList.create({}) const {_id, email, verificationKey: key, theme, language} = await User.create({...req.body, noteList}) if (!email) { throw new Error('Something went wrong') } sendMail(req.body.email, subject, text(key), html(key)) const user = {_id, email, noteList, theme, language, isVerified: false, isLoggedIn: true} req.session.set('user', user) await req.session.save() res.status(201).json(user) } catch (error) { console.log(error) res.status(400).json({isLoggedIn: false}) } break default: res.status(400).json({isLoggedIn: false}) break } })