Skip to content

Commit

Permalink
Merge pull request #20 from tanish35/master
Browse files Browse the repository at this point in the history
Added custom .json file to add more domains
  • Loading branch information
tanish35 authored Jul 30, 2024
2 parents 0e76094 + 880f9bf commit 9e176a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backend/src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } =
Expand All @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions backend/src/mail/checkAcademic.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions backend/src/mail/emails.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"aec.edu",
"jadavpuruniversity.in",
"anothercollege.edu"
]

0 comments on commit 9e176a3

Please sign in to comment.