From cc5c0ea5a3dddda2d15dcec768d0fde0537b47a0 Mon Sep 17 00:00:00 2001 From: holabayor Date: Sun, 24 Sep 2023 08:49:01 +0100 Subject: [PATCH] confirm invite error handling --- controllers/organizationController.js | 49 ++++++++++----------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/controllers/organizationController.js b/controllers/organizationController.js index f945224e..99cc0530 100644 --- a/controllers/organizationController.js +++ b/controllers/organizationController.js @@ -92,40 +92,25 @@ const confirmInviteCode = async (req, res, next) => { where: { token: verificationCode }, }); - if (invite) { - res.email = invite.email; - res.org_id = invite.org_id; - res.status(200).json({ - success: true, - message: 'Token verified', - data: { - org_id: invite.org_id, - email: invite.email, - }, - }); - } else { - createCustomError('Invalid verification code', 400); + if (!invite) { + throw createCustomError('Invalid verification code', 400); } - // // Verifing the verification code against the stored code in your database - // const user = await orgInvites.findOne({ - // where: { email, token: verificationCode }, - // }); - - // if (!user || user.token !== verificationCode) { - // return res.status(400).json({ - // success: false, - // message: 'Invalid email or verification code.', - // data: null, - // }); - // } - - // // Mark the email as verified - // user.email = true; - // user.token = null; // Optional, clear the verification code from the database or not - // // There is supposed to be a field where we set the state to be true once token is validated - - // await user.save(); + // Delete the verification code from the database + await orgInvites.destroy({ + where: { token: verificationCode }, + }); + + res.email = invite.email; + res.org_id = invite.org_id; + res.status(200).json({ + success: true, + message: 'Token verified', + data: { + org_id: invite.org_id, + email: invite.email, + }, + }); } catch (error) { next(error); }