Skip to content

Commit

Permalink
implemented backend validation for valid email and domain
Browse files Browse the repository at this point in the history
  • Loading branch information
denish-fearless committed Jan 15, 2025
1 parent 0cf8aee commit 37b3b2b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"db-migrate": "^0.11.11",
"db-migrate-pg": "^1.2.2",
"dotenv": "^16.0.3",
"email-validator": "^2.0.4",
"express": "^4.17.1",
"express-rate-limit": "^7.2.0",
"express-validator": "^7.0.1",
Expand Down
45 changes: 45 additions & 0 deletions backend/src/middleware/emailValidator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { isValidEmail } from './emailValidator';
import dns from 'dns';

jest.mock('dns');

const mockedDns = dns as jest.Mocked<typeof dns>;

describe('isValidEmail Function', () => {

afterEach(() => {
jest.resetAllMocks();
});

test('should return false for an invalid email format', async () => {
const result = await isValidEmail('invalid-email');
expect(result).toBe(false);
});

test('should return true for a valid email with MX records', async () => {
mockedDns.resolveMx.mockImplementation((domain, callback) => {
callback(null, [{ exchange: 'mail.google.com', priority: 10 }]);
});

const result = await isValidEmail('[email protected]');
expect(result).toBe(true);
});

test('should return false for a valid email but no MX records', async () => {
mockedDns.resolveMx.mockImplementation((domain, callback) => {
callback(null, []);
});

const result = await isValidEmail('[email protected]');
expect(result).toBe(false);
});

test('should return false for a domain with an error during MX lookup', async () => {
mockedDns.resolveMx.mockImplementation((domain, callback) => {
callback(new Error('Domain not found'), []);
});

const result = await isValidEmail('[email protected]');
expect(result).toBe(false);
});
});
33 changes: 33 additions & 0 deletions backend/src/middleware/emailValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import dns from 'dns';
import * as EmailValidator from 'email-validator';

export const isValidEmail = async (email: string): Promise<boolean> => {
if (!EmailValidator.validate(email)) {
console.log("Invalid email format");
return false;
}

try {
const isValidDomain = await validateDomainMX(email);
return isValidDomain;
} catch (error) {
console.error("Error during MX record validation:", error);
return false;
}
};

const validateDomainMX = (email: string): Promise<boolean> => {
const domain = email.split('@')[1];
return new Promise((resolve) => {
dns.resolveMx(domain, (err, addresses) => {
if (err || addresses.length === 0) {
console.log(`Invalid domain or no MX records for: ${domain}`);
resolve(false);
} else {
console.log(`Valid MX records found for ${domain}:`, addresses);
resolve(true);
}
});
});
};

0 comments on commit 37b3b2b

Please sign in to comment.