diff --git a/backend/src/controllers/userControllers.ts b/backend/src/controllers/userControllers.ts index f5ffaab..008a8ef 100644 --- a/backend/src/controllers/userControllers.ts +++ b/backend/src/controllers/userControllers.ts @@ -5,6 +5,7 @@ import jwt from "jsonwebtoken"; import bcrypt from "bcrypt"; import sendMail from "../mail/sendMail"; import { Verifier } from "academic-email-verifier"; +import checkCollegeEmail from "../mail/checkAcademic"; const registerUser = asyncHandler(async (req: Request, res: Response) => { let { email, name, password, collegeName, courseName, isOnline, location } = @@ -24,10 +25,9 @@ const registerUser = asyncHandler(async (req: Request, res: Response) => { return; } - const regex = /@jadavpuruniversity\.in$/; let isCollegeEmail; - - if (regex.test(email)) { + + if (checkCollegeEmail(email)) { isCollegeEmail = true; } else { isCollegeEmail = await Verifier.isAcademic(email); diff --git a/backend/src/mail/checkAcademic.ts b/backend/src/mail/checkAcademic.ts new file mode 100644 index 0000000..42068c5 --- /dev/null +++ b/backend/src/mail/checkAcademic.ts @@ -0,0 +1,19 @@ +import fs from 'fs'; +import path from 'path'; + +// Get the directory name of the current module +const __dirname = path.resolve(); // CommonJS way to get the directory path + +// Function to check if an email has a valid college domain +function checkCollegeEmail(email: string): boolean { + const filePath = path.join(__dirname, 'emails.json'); + const domains = JSON.parse(fs.readFileSync(filePath, 'utf8')); + + // Extract the domain from the email + const emailDomain = email.split('@')[1]; + + // Check if the email domain is in the list of college domains + return domains.includes(emailDomain); +} + +export default checkCollegeEmail; diff --git a/backend/src/mail/emails.json b/backend/src/mail/emails.json new file mode 100644 index 0000000..a1fc90b --- /dev/null +++ b/backend/src/mail/emails.json @@ -0,0 +1,6 @@ +[ + "aec.edu", + "jadavpuruniversity.in", + "anothercollege.edu" +] + \ No newline at end of file