-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dev Patel
authored and
Dev Patel
committed
Jul 22, 2024
1 parent
971dc28
commit 0b11ee7
Showing
8 changed files
with
14,439 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtz | ||
c2gtZWQyNTUxOQAAACDhH2spW9yFyOaa7rxWW6tHP8bRZqeGtQo7T3nZYFuUBQAA | ||
AIjOb/nezm/53gAAAAtzc2gtZWQyNTUxOQAAACDhH2spW9yFyOaa7rxWW6tHP8bR | ||
ZqeGtQo7T3nZYFuUBQAAAEAwUQIBATAFBgMrZXAEIgQgc2wu+olXlSihWByE7lIf | ||
4uEfaylb3IXI5pruvFZbq0c/xtFmp4a1CjtPedlgW5QFAAAAAAECAwQF | ||
-----END OPENSSH PRIVATE KEY----- |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { Router } from "express"; | ||
import { Database } from "../../database"; | ||
import RoleChecker from "../../middleware/role-checker"; | ||
import { Role } from "../auth/auth-models"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { SponsorValidator } from "./sponsor-schema"; | ||
import {sendEmail} from "../ses/ses-utils" | ||
import jsonwebtoken from "jsonwebtoken"; | ||
import { Config } from "../../config"; | ||
const bcrypt = require('bcrypt'); | ||
Check failure on line 10 in src/services/sponsor/sponsor-router.ts GitHub Actions / lint
|
||
const sponsorRouter = Router(); | ||
|
||
sponsorRouter.get('/test', (req, res) => { | ||
res.status(200).send({ message: 'Route found' }); | ||
}); | ||
|
||
// 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); | ||
} | ||
} | ||
); | ||
|
||
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) { | ||
console.log("SixDijit: ", sixDigitCode) | ||
const saltRounds = 10; | ||
|
||
try { | ||
const hash = bcrypt.hashSync(sixDigitCode, saltRounds); | ||
return hash | ||
} catch (err) { | ||
console.error('Error encrypting the code:', err); | ||
throw err; | ||
} | ||
} | ||
|
||
sponsorRouter.post( | ||
"/login", | ||
async (req, res, next) => { | ||
const { email } = req.body; | ||
try { | ||
console.log("email: ", email) | ||
console.log("req: ", req.body) | ||
|
||
const sixDigitCode = createSixDigitCode(); | ||
console.log("SixDijit: ", sixDigitCode) | ||
const expTime = Math.floor(Date.now() / 1000) + 120; //2 minutes | ||
console.log("expTime: ", expTime) | ||
const hashedVerificationCode = encryptSixDigitCode(sixDigitCode); | ||
console.log("hashed code: ", hashedVerificationCode) | ||
//create auth collection | ||
//store hashedCode and expTime in mongo auth collection | ||
const validatedData = SponsorValidator.parse({email, hashedVerificationCode, expTime}); | ||
console.log("test") | ||
const sponsor = new Database.SPONSOR(validatedData); | ||
await sponsor.save(); | ||
//send verification email to email adress | ||
await sendEmail(email, 'RP-Verify your Email', ` Verifiction Code: ${sixDigitCode}`); | ||
return res.sendStatus(StatusCodes.CREATED); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
sponsorRouter.post( | ||
"/verify", | ||
async (req, res, next) => { | ||
const { email, sixDigitCode } = req.body; | ||
try { | ||
|
||
const sponsorData = await Database.SPONSOR.findOne({ email: email }); | ||
const { hashedVerificationCode, expTime } = sponsorData | ||
Check failure on line 99 in src/services/sponsor/sponsor-router.ts GitHub Actions / build
|
||
if (new Date() > expTime){ | ||
return res.status(401).json({ message: 'Code expired' }); | ||
} | ||
const match = await bcrypt.compareSync(sixDigitCode, 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_SECRE, | ||
Check failure on line 114 in src/services/sponsor/sponsor-router.ts GitHub Actions / build
|
||
{ | ||
expiresIn: Config.JWT_EXPIRATION_TIME, | ||
} | ||
); | ||
res.json({ token }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
export default sponsorRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import mongoose from "mongoose"; | ||
import { z } from "zod"; | ||
|
||
// // Zod schema for registration | ||
// export const AuthValidator = z.object({ | ||
// email: z.string().email(), | ||
// hashedVerificationCode: z.string(), | ||
// expTime: z.number() | ||
// }); | ||
|
||
// // Mongoose schema for registration | ||
// export const AuthSchema = new mongoose.Schema({ | ||
// email: { type: String, required: true, unique: true }, | ||
// hashedVerificationCode: { type: String, required: true }, | ||
// expTime: { type: Number, required: true } // Add this line to include expTime | ||
// }); | ||
|
||
export const SponsorSchema = new mongoose.Schema({ | ||
email: { type: String, required: true, unique: true }, | ||
hashedVerificationCode: { type: String, required: true }, // Ensure this matches your zod schema | ||
expTime: { type: Number, required: true } | ||
}); | ||
|
||
export const SponsorValidator = z.object({ | ||
email: z.string().email(), | ||
hashedVerificationCode: z.string(), // Ensure this matches your mongoose schema | ||
expTime: z.number().int() | ||
}); |
Oops, something went wrong.