From 049519d08cb0f5665272cc2f751e4d65357cba16 Mon Sep 17 00:00:00 2001 From: Dev Patel <63603475+Patle1234@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:07:14 -0500 Subject: [PATCH] linter annoying --- src/services/sponsor/sponsor-router.ts | 117 +++++++++++++------------ 1 file changed, 59 insertions(+), 58 deletions(-) diff --git a/src/services/sponsor/sponsor-router.ts b/src/services/sponsor/sponsor-router.ts index d1b55eb..f3e101a 100644 --- a/src/services/sponsor/sponsor-router.ts +++ b/src/services/sponsor/sponsor-router.ts @@ -3,10 +3,10 @@ import { Database } from "../../database"; import RoleChecker from "../../middleware/role-checker"; import { Role } from "../auth/auth-models"; import { StatusCodes } from "http-status-codes"; -import {sendEmail} from "../ses/ses-utils" +import { sendEmail } from "../ses/ses-utils"; import jsonwebtoken from "jsonwebtoken"; import { Config } from "../../config"; -const bcrypt = require('bcrypt'); +const bcrypt = require("bcrypt"); const sponsorRouter = Router(); // Get favorite events for an attendee @@ -34,8 +34,8 @@ sponsorRouter.get( ); function createSixDigitCode() { - let result = ''; - const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + let result = ""; + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (let i = 0; i < 6; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } @@ -50,70 +50,71 @@ function encryptSixDigitCode(sixDigitCode: string): string { const hash = bcrypt.hashSync(sixDigitCode, saltRounds); return hash; } catch (err) { - console.error('Error encrypting the code:', err); + console.error("Error encrypting the code:", err); throw err; } } -sponsorRouter.post( - "/login", - async (req, res, next) => { - const { email } = req.body; - try { - const sixDigitCode = createSixDigitCode(); - const expTime = Math.floor(Date.now() / 1000) + 300; - const hashedVerificationCode = encryptSixDigitCode(sixDigitCode); - await Database.SPONSOR.findOneAndUpdate( - { email }, - { - $set: { +sponsorRouter.post("/login", async (req, res, next) => { + const { email } = req.body; + try { + const sixDigitCode = createSixDigitCode(); + const expTime = Math.floor(Date.now() / 1000) + 300; + const hashedVerificationCode = encryptSixDigitCode(sixDigitCode); + await Database.SPONSOR.findOneAndUpdate( + { email }, + { + $set: { hashedVerificationCode: hashedVerificationCode, - expTime: expTime, - }, + expTime: expTime, }, - { upsert: true } - ); - await sendEmail(email, 'RP-Sponor Email Verification!', `Here is your verification code: ${sixDigitCode}`); - return res.sendStatus(StatusCodes.CREATED); - } catch (error) { - next(error); - } + }, + { upsert: true } + ); + await sendEmail( + email, + "RP-Sponor Email Verification!", + `Here is your verification code: ${sixDigitCode}` + ); + return res.sendStatus(StatusCodes.CREATED); + } catch (error) { + next(error); } -); +}); -sponsorRouter.post( - "/verify", - async (req, res, next) => { - const { email, sixDigitCodeInput } = req.body; - try { - const sponsorData = await Database.SPONSOR.findOne({ email }); - if (!sponsorData) { - return res.status(401).json({ message: 'No Access' }); - } - const { hashedVerificationCode, expTime } = sponsorData - if (Math.floor(Date.now() / 1000) > expTime){ - return res.status(401).json({ message: 'Code expired' }); - } - const match = await bcrypt.compareSync(sixDigitCodeInput, hashedVerificationCode) - if (!match) { - return res.status(401).json({ message: 'Incorrect Code' }); - } - await Database.SPONSOR.deleteOne({ email }); - const token = jsonwebtoken.sign( - { +sponsorRouter.post("/verify", async (req, res, next) => { + const { email, sixDigitCodeInput } = req.body; + try { + const sponsorData = await Database.SPONSOR.findOne({ email }); + if (!sponsorData) { + return res.status(401).json({ message: "No Access" }); + } + const { hashedVerificationCode, expTime } = sponsorData; + if (Math.floor(Date.now() / 1000) > expTime) { + return res.status(401).json({ message: "Code expired" }); + } + const match = await bcrypt.compareSync( + sixDigitCodeInput, + hashedVerificationCode + ); + if (!match) { + return res.status(401).json({ message: "Incorrect Code" }); + } + await Database.SPONSOR.deleteOne({ email }); + const token = jsonwebtoken.sign( + { email, - role: 'CORPORATE' - }, - Config.JWT_SIGNING_SECRET, - { + role: "CORPORATE", + }, + Config.JWT_SIGNING_SECRET, + { expiresIn: Config.JWT_EXPIRATION_TIME, - } - ); - res.json({ token }); - } catch (error) { - next(error); - } + } + ); + res.json({ token }); + } catch (error) { + next(error); } -); +}); export default sponsorRouter;