From 8d65c5821a833859c634080a6d90a0bc4a35b01f Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 11 Jun 2023 13:43:02 +0300 Subject: [PATCH] email validator added and implemented --- README.md | 1 + package-lock.json | 35 +++++++++++++++++++++++++++++++ package.json | 5 +++-- src/controllers/UserController.ts | 12 +++++++++-- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 39a7d91..8e72d56 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ added the required error handling for the application. - Docker (docker-compose) - bcrypt - JWT +- deep-email-validator ## How to Run diff --git a/package-lock.json b/package-lock.json index 38de821..d35178c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@sendgrid/mail": "^7.7.0", "bcryptjs": "^2.4.3", "cookie-parser": "^1.4.6", + "deep-email-validator": "^0.1.21", "dotenv": "^16.1.4", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", @@ -95,6 +96,11 @@ "@types/express": "*" } }, + "node_modules/@types/disposable-email-domains": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@types/disposable-email-domains/-/disposable-email-domains-1.0.4.tgz", + "integrity": "sha512-AmKPD8vBZzvey/jeg+YAIH/xJE3D6edOXz+YUooSCcHesGzFyzke83kj1j4d0LUR9nkSHIRklUVdcAMleuWLpg==" + }, "node_modules/@types/express": { "version": "4.17.17", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz", @@ -420,6 +426,25 @@ "ms": "2.0.0" } }, + "node_modules/deep-email-validator": { + "version": "0.1.21", + "resolved": "https://registry.npmjs.org/deep-email-validator/-/deep-email-validator-0.1.21.tgz", + "integrity": "sha512-DBAmMzbr+MAubXQ+TS9tZuPwLcdKscb8YzKZiwoLqF3NmaeEgXvSSHhZ0EXOFeKFE2FNWC4mNXCyiQ/JdFXUwg==", + "dependencies": { + "@types/disposable-email-domains": "^1.0.1", + "axios": "^0.24.0", + "disposable-email-domains": "^1.0.59", + "mailcheck": "^1.1.1" + } + }, + "node_modules/deep-email-validator/node_modules/axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "dependencies": { + "follow-redirects": "^1.14.4" + } + }, "node_modules/deepmerge": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", @@ -445,6 +470,11 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/disposable-email-domains": { + "version": "1.0.62", + "resolved": "https://registry.npmjs.org/disposable-email-domains/-/disposable-email-domains-1.0.62.tgz", + "integrity": "sha512-LBQvhRw7mznQTPoyZbsmYeNOZt1pN5aCsx4BAU/3siVFuiM9f2oyKzUaB8v1jbxFjE3aYqYiMo63kAL4pHgfWQ==" + }, "node_modules/dotenv": { "version": "16.1.4", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.4.tgz", @@ -837,6 +867,11 @@ "node": ">=10" } }, + "node_modules/mailcheck": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/mailcheck/-/mailcheck-1.1.1.tgz", + "integrity": "sha512-3WjL8+ZDouZwKlyJBMp/4LeziLFXgleOdsYu87piGcMLqhBzCsy2QFdbtAwv757TFC/rtqd738fgJw1tFQCSgA==" + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", diff --git a/package.json b/package.json index 6593ad7..815fdef 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,15 @@ "scripts": { "dev": "nodemon dist/index.js", "start": "node dist/index.js", - "build": "tsc -p ." - }, + "build": "tsc -p ." + }, "author": "", "license": "ISC", "dependencies": { "@sendgrid/mail": "^7.7.0", "bcryptjs": "^2.4.3", "cookie-parser": "^1.4.6", + "deep-email-validator": "^0.1.21", "dotenv": "^16.1.4", "express": "^4.18.2", "jsonwebtoken": "^9.0.0", diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index c55b268..30f010c 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -3,12 +3,18 @@ import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; import { User, IUser } from '../mongoose/Schema'; import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth'; +import validate from 'deep-email-validator'; export async function createUser(req: Request, res: Response) { try { const { email, password, address } = req.body; - - if (!(email && password && address)) { + const isValidEmail = await validate(email); + if (!isValidEmail.valid) { + console.error('Email is invalid:', isValidEmail.validators); + return res.status(400).json({ error: 'Email is invalid' }); + } + + if (!(password && address)) { return res.status(400).json({ error: 'All inputs are required' }); } // checkIfUserExists return true if the user exists @@ -41,12 +47,14 @@ export async function login(req: Request, res: Response) { // Check if the user exists const user: IUser | null = await User.findOne({ email }); if (!user) { + console.error('User not found'); return res.status(401).json({ error: 'Invalid email or password' }); } // Compare the provided password with the stored password const isPasswordCorrect = await bcrypt.compare(password, user.password); if (!isPasswordCorrect) { + console.error('Invalid password'); return res.status(401).json({ error: 'Invalid email or password' }); }