aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api/verify.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-08-09 21:36:03 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-08-09 21:37:03 +0200
commit464e470441287572cfda8d95484f781236b9db35 (patch)
tree87177837cb6ee6ee000f0d39fa5ba7ee6bb2943e /pages/api/verify.js
downloadmy_apps-464e470441287572cfda8d95484f781236b9db35.tar.gz
my_apps-464e470441287572cfda8d95484f781236b9db35.tar.bz2
my_apps-464e470441287572cfda8d95484f781236b9db35.zip
init commit
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
+ }
+})
+