-
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.
Merge pull request #113 from ReflectionsProjections/dev/devp/sponsor
Sponsor Endpoints
- Loading branch information
Showing
9 changed files
with
480 additions
and
90 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
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
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,89 @@ | ||
import { Router } from "express"; | ||
import { Database } from "../../../database"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { sendEmail } from "../../ses/ses-utils"; | ||
import jsonwebtoken from "jsonwebtoken"; | ||
import { Config } from "../../../config"; | ||
import { Role } from "../../auth/auth-models"; | ||
import { | ||
createSixDigitCode, | ||
encryptSixDigitCode, | ||
sponsorExists, | ||
} from "./sponsor-utils"; | ||
import * as bcrypt from "bcrypt"; | ||
import { | ||
AuthSponsorLoginValidator, | ||
AuthSponsorVerifyValidator, | ||
} from "./sponsor-schema"; | ||
|
||
const authSponsorRouter = Router(); | ||
|
||
authSponsorRouter.post("/login", async (req, res, next) => { | ||
try { | ||
const { email } = AuthSponsorLoginValidator.parse(req.body); | ||
if (!(await sponsorExists(email))) { | ||
return res.sendStatus(StatusCodes.UNAUTHORIZED); | ||
} | ||
|
||
const sixDigitCode = createSixDigitCode(); | ||
const expTime = | ||
Math.floor(Date.now() / 1000) + Config.VERIFY_EXP_TIME_MS; | ||
const hashedVerificationCode = encryptSixDigitCode(sixDigitCode); | ||
await Database.AUTH_CODES.findOneAndUpdate( | ||
{ email }, | ||
{ | ||
hashedVerificationCode, | ||
expTime, | ||
}, | ||
{ upsert: true } | ||
); | ||
await sendEmail( | ||
email, | ||
"R|P Sponsor Email Verification!", | ||
`Here is your verification code: ${sixDigitCode}` | ||
); | ||
return res.sendStatus(StatusCodes.CREATED); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
authSponsorRouter.post("/verify", async (req, res, next) => { | ||
try { | ||
const { email, sixDigitCode } = AuthSponsorVerifyValidator.parse( | ||
req.body | ||
); | ||
const sponsorData = await Database.AUTH_CODES.findOneAndDelete({ | ||
email, | ||
}); | ||
if (!sponsorData) { | ||
return res.sendStatus(StatusCodes.UNAUTHORIZED); | ||
} | ||
if (Math.floor(Date.now() / 1000) > sponsorData.expTime) { | ||
return res.sendStatus(StatusCodes.GONE); | ||
} | ||
const match = bcrypt.compareSync( | ||
sixDigitCode, | ||
sponsorData.hashedVerificationCode | ||
); | ||
if (!match) { | ||
return res.sendStatus(StatusCodes.UNAUTHORIZED); | ||
} | ||
const token = jsonwebtoken.sign( | ||
{ | ||
email, | ||
role: Role.Enum.CORPORATE, | ||
}, | ||
Config.JWT_SIGNING_SECRET, | ||
{ | ||
expiresIn: | ||
Math.floor(Date.now() / 1000) + Config.JWT_EXPIRATION_TIME, | ||
} | ||
); | ||
return res.status(StatusCodes.OK).json({ token }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
export default authSponsorRouter; |
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,23 @@ | ||
import mongoose from "mongoose"; | ||
import { z } from "zod"; | ||
|
||
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 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), | ||
}); |
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 * as bcrypt from "bcrypt"; | ||
import { Database } from "../../../database"; | ||
import { Config } from "../../../config"; | ||
|
||
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 { | ||
try { | ||
const hash = bcrypt.hashSync(sixDigitCode, Config.HASH_SALT_ROUNDS); | ||
return hash; | ||
} catch (err) { | ||
console.error("Error encrypting the code:", err); | ||
throw err; | ||
} | ||
} | ||
|
||
export async function sponsorExists(email: string) { | ||
const response = await Database.CORPORATE.findOne({ email: email }); | ||
if (!response) return false; | ||
return true; | ||
} |
Oops, something went wrong.