Compare commits

..

No commits in common. "8d65c5821a833859c634080a6d90a0bc4a35b01f" and "28ccd1fe6440c210fb6c0fcc341f3b2de0dada07" have entirely different histories.

5 changed files with 11 additions and 63 deletions

View file

@ -39,7 +39,6 @@ added the required error handling for the application.
- Docker (docker-compose)
- bcrypt
- JWT
- deep-email-validator
## How to Run
@ -219,17 +218,4 @@ status 404
message:
"Cart not found."
}
```
# Database Schema
## User
```
{
name: string,
email: string,
password: string,
cart: {
productId: number
}
}
```

35
package-lock.json generated
View file

@ -12,7 +12,6 @@
"@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",
@ -96,11 +95,6 @@
"@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",
@ -426,25 +420,6 @@
"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",
@ -470,11 +445,6 @@
"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",
@ -867,11 +837,6 @@
"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",

View file

@ -6,15 +6,14 @@
"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",

View file

@ -3,18 +3,12 @@ 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;
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)) {
const { firstName, lastName, email, password, address } = req.body;
if (!(email && password && firstName && lastName && address)) {
return res.status(400).json({ error: 'All inputs are required' });
}
// checkIfUserExists return true if the user exists
@ -26,6 +20,8 @@ export async function createUser(req: Request, res: Response) {
const hashedPassword = await bcrypt.hash(password, 10);
const user: IUser = await User.create({
firstName,
lastName,
email,
password: hashedPassword,
address,
@ -47,14 +43,12 @@ 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' });
}

View file

@ -1,6 +1,8 @@
import mongoose, { Schema, Document } from 'mongoose';
export interface IUser extends Document {
firstName: string;
lastName: string;
email: string;
password: string;
address: string;
@ -33,6 +35,8 @@ export interface IOrder extends Document {
}
const UserSchema: Schema = new Schema({
firstName: { type: String, required: true },
lastName: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
address: { type: String, required: true },