aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/verify.js
diff options
context:
space:
mode:
Diffstat (limited to 'pages/api/verify.js')
-rw-r--r--pages/api/verify.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/pages/api/verify.js b/pages/api/verify.js
new file mode 100644
index 0000000..1606dbc
--- /dev/null
+++ b/pages/api/verify.js
@@ -0,0 +1,45 @@
+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
+ }
+})
+