Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patle1234 committed Jul 25, 2024
1 parent 18a6420 commit 92fa89a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13,850 deletions.
13,784 changes: 0 additions & 13,784 deletions package-lock.json

This file was deleted.

2 changes: 0 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import s3Router from "./services/s3/s3-router";
import statsRouter from "./services/stats/stats-router";
import subscriptionRouter from "./services/subscription/subscription-router";
import speakersRouter from "./services/speakers/speakers-router";
// import sponsorRouter from "./services/sponsor/sponsor-router";

AWS.config.update({
region: Config.S3_REGION,
Expand Down Expand Up @@ -55,7 +54,6 @@ app.use("/s3", databaseMiddleware, s3Router);
app.use("/stats", databaseMiddleware, statsRouter);
app.use("/subscription", databaseMiddleware, subscriptionRouter);
app.use("/speakers", databaseMiddleware, speakersRouter);
// app.use("/sponsor", databaseMiddleware, sponsorRouter);

app.get("/status", (_, res) => {
return res.status(StatusCodes.OK).send("API is alive!");
Expand Down
6 changes: 3 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import {
SpeakerValidator,
} from "./services/speakers/speakers-schema";
import {
SponsorSchema,
SponsorValidator,
SponsorAuthSchema,
SponsorAuthValidator,
} from "./services/auth/sponsor/sponsor-schema";
import {
CorporateSchema,
Expand Down Expand Up @@ -101,7 +101,7 @@ export const Database = {
NotificationsSchema,
NotificationsValidator
),
AUTH_CODES: initializeModel("auth_codes", SponsorSchema, SponsorValidator),
AUTH_CODES: initializeModel("auth_codes", SponsorAuthSchema, SponsorAuthValidator),

Check failure on line 104 in src/database.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"auth_codes",·SponsorAuthSchema,·SponsorAuthValidator` with `⏎········"auth_codes",⏎········SponsorAuthSchema,⏎········SponsorAuthValidator⏎····`
SPEAKERS: initializeModel("speakers", SpeakerSchema, SpeakerValidator),
CORPORATE: initializeModel(
"corporate",
Expand Down
72 changes: 13 additions & 59 deletions src/services/auth/sponsor/sponsor-router.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,32 @@
import { Router } from "express";
import { Database } from "../../../database";
import RoleChecker from "../../../middleware/role-checker";
import { Role } from "../auth-models";
import { StatusCodes } from "http-status-codes";
import { sendEmail } from "../../ses/ses-utils";
import jsonwebtoken from "jsonwebtoken";
import { Config } from "../../../config";
import { createSixDigitCode, encryptSixDigitCode} from "./sponsor-utils";

Check failure on line 7 in src/services/auth/sponsor/sponsor-router.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `·`
import * as bcrypt from "bcrypt";
const sponsorRouter = Router();

// Get favorite events for an attendee
sponsorRouter.get(
"/",
RoleChecker([Role.Enum.CORPORATE]),
async (req, res, next) => {
try {
const resumeUsers = await Database.REGISTRATION.find(
{ hasResume: true },
{ userId: 1 }
);
if (!resumeUsers) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}
return res.status(StatusCodes.OK).json(resumeUsers);
} catch (error) {
next(error);
}
}
);
import {AuthSponsorLoginValidator, AuthSponsorVerifyValidator} from "./sponsor-schema";

Check failure on line 9 in src/services/auth/sponsor/sponsor-router.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `AuthSponsorLoginValidator,·AuthSponsorVerifyValidator` with `⏎····AuthSponsorLoginValidator,⏎····AuthSponsorVerifyValidator,⏎`

function createSixDigitCode() {
let result = "";
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (let i = 0; i < 6; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

function encryptSixDigitCode(sixDigitCode: string): string {
console.log("SixDigit: ", sixDigitCode);
const saltRounds = 10;

try {
const hash = bcrypt.hashSync(sixDigitCode, saltRounds);
return hash;
} catch (err) {
console.error("Error encrypting the code:", err);
throw err;
}
}
const sponsorRouter = Router();

sponsorRouter.post("/login", async (req, res, next) => {
const { email } = req.body;
try {
const { email } = AuthSponsorLoginValidator.parse(req.body);
const sixDigitCode = createSixDigitCode();
const expTime = Math.floor(Date.now() / 1000) + 300;
const hashedVerificationCode = encryptSixDigitCode(sixDigitCode);
await Database.AUTH_CODES.findOneAndUpdate(
{ email },
{
$set: {
hashedVerificationCode: hashedVerificationCode,
expTime: expTime,
},
hashedVerificationCode: hashedVerificationCode,
expTime: expTime,
},
{ upsert: true }
);
await sendEmail(
email,
"RP-Sponor Email Verification!",
"R|P Sponsor Email Verification!",
`Here is your verification code: ${sixDigitCode}`
);
return res.sendStatus(StatusCodes.CREATED);
Expand All @@ -81,32 +36,31 @@ sponsorRouter.post("/login", async (req, res, next) => {
});

sponsorRouter.post("/verify", async (req, res, next) => {
const { email, sixDigitCode } = req.body;
try {
const sponsorData = await Database.AUTH_CODES.findOne({ email });
const { email, sixDigitCode } = AuthSponsorVerifyValidator.parse(req.body);

Check failure on line 40 in src/services/auth/sponsor/sponsor-router.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `req.body` with `⏎············req.body⏎········`
const sponsorData = await Database.AUTH_CODES.findOneAndDelete({ email });

Check failure on line 41 in src/services/auth/sponsor/sponsor-router.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·email` with `⏎············email,⏎·······`
if (!sponsorData) {
return res.status(401).json({ message: "No Access" });
return res.sendStatus(StatusCodes.UNAUTHORIZED);
}
const { hashedVerificationCode, expTime } = sponsorData;
if (Math.floor(Date.now() / 1000) > expTime) {
return res.status(401).json({ message: "Code expired" });
return res.sendStatus(StatusCodes.GONE);
}
const match = await bcrypt.compareSync(
sixDigitCode,
hashedVerificationCode
);
if (!match) {
return res.status(401).json({ message: "Incorrect Code" });
return res.sendStatus(StatusCodes.BAD_REQUEST);
}
await Database.AUTH_CODES.deleteOne({ email });
const token = jsonwebtoken.sign(
{
email,
role: "CORPORATE",
},
Config.JWT_SIGNING_SECRET,
{
expiresIn: Config.JWT_EXPIRATION_TIME,
expiresIn: (Math.floor(Date.now() / 1000)) + Config.JWT_EXPIRATION_TIME

Check failure on line 63 in src/services/auth/sponsor/sponsor-router.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·(Math.floor(Date.now()·/·1000))·+·Config.JWT_EXPIRATION_TIME` with `⏎····················Math.floor(Date.now()·/·1000)·+·Config.JWT_EXPIRATION_TIME,`
}
);
res.json({ token });
Expand Down
13 changes: 11 additions & 2 deletions src/services/auth/sponsor/sponsor-schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import mongoose from "mongoose";
import { z } from "zod";

export const SponsorSchema = new mongoose.Schema({
export const SponsorAuthSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
hashedVerificationCode: { type: String, required: true },
expTime: { type: Number, required: true },
});

export const SponsorValidator = z.object({
export const SponsorAuthValidator = z.object({
email: z.string().email(),
hashedVerificationCode: z.string(),
expTime: z.number().int(),
});

export const AuthSponsorLoginValidator = z.object({
email: z.string().email(),
});

export const AuthSponsorVerifyValidator = z.object({
email: z.string().email(),
sixDigitCode: z.string().length(6)

Check failure on line 22 in src/services/auth/sponsor/sponsor-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
});

Check failure on line 23 in src/services/auth/sponsor/sponsor-schema.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`
23 changes: 23 additions & 0 deletions src/services/auth/sponsor/sponsor-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as bcrypt from "bcrypt";


Check failure on line 3 in src/services/auth/sponsor/sponsor-utils.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
export function createSixDigitCode() {
let result = "";
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (let i = 0; i < 6; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

export function encryptSixDigitCode(sixDigitCode: string): string {
const saltRounds = 10;

try {
const hash = bcrypt.hashSync(sixDigitCode, saltRounds);
return hash;
} catch (err) {
console.error("Error encrypting the code:", err);
throw err;
}
}

0 comments on commit 92fa89a

Please sign in to comment.