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) - Docker (docker-compose)
- bcrypt - bcrypt
- JWT - JWT
- deep-email-validator
## How to Run ## How to Run
@ -220,16 +219,3 @@ status 404
"Cart not found." "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", "@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"deep-email-validator": "^0.1.21",
"dotenv": "^16.1.4", "dotenv": "^16.1.4",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
@ -96,11 +95,6 @@
"@types/express": "*" "@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": { "node_modules/@types/express": {
"version": "4.17.17", "version": "4.17.17",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz",
@ -426,25 +420,6 @@
"ms": "2.0.0" "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": { "node_modules/deepmerge": {
"version": "4.3.1", "version": "4.3.1",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
@ -470,11 +445,6 @@
"npm": "1.2.8000 || >= 1.4.16" "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": { "node_modules/dotenv": {
"version": "16.1.4", "version": "16.1.4",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.4.tgz", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.1.4.tgz",
@ -867,11 +837,6 @@
"node": ">=10" "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": { "node_modules/media-typer": {
"version": "0.3.0", "version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",

View file

@ -14,7 +14,6 @@
"@sendgrid/mail": "^7.7.0", "@sendgrid/mail": "^7.7.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"deep-email-validator": "^0.1.21",
"dotenv": "^16.1.4", "dotenv": "^16.1.4",
"express": "^4.18.2", "express": "^4.18.2",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",

View file

@ -3,18 +3,12 @@ import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import { User, IUser } from '../mongoose/Schema'; import { User, IUser } from '../mongoose/Schema';
import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth'; import { clearJwtCookie, setJwtCookie } from '../middlewares/checkAuth';
import validate from 'deep-email-validator';
export async function createUser(req: Request, res: Response) { export async function createUser(req: Request, res: Response) {
try { try {
const { email, password, address } = req.body; const { firstName, lastName, 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)) { if (!(email && password && firstName && lastName && address)) {
return res.status(400).json({ error: 'All inputs are required' }); return res.status(400).json({ error: 'All inputs are required' });
} }
// checkIfUserExists return true if the user exists // 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 hashedPassword = await bcrypt.hash(password, 10);
const user: IUser = await User.create({ const user: IUser = await User.create({
firstName,
lastName,
email, email,
password: hashedPassword, password: hashedPassword,
address, address,
@ -47,14 +43,12 @@ export async function login(req: Request, res: Response) {
// Check if the user exists // Check if the user exists
const user: IUser | null = await User.findOne({ email }); const user: IUser | null = await User.findOne({ email });
if (!user) { if (!user) {
console.error('User not found');
return res.status(401).json({ error: 'Invalid email or password' }); return res.status(401).json({ error: 'Invalid email or password' });
} }
// Compare the provided password with the stored password // Compare the provided password with the stored password
const isPasswordCorrect = await bcrypt.compare(password, user.password); const isPasswordCorrect = await bcrypt.compare(password, user.password);
if (!isPasswordCorrect) { if (!isPasswordCorrect) {
console.error('Invalid password');
return res.status(401).json({ error: 'Invalid email or password' }); return res.status(401).json({ error: 'Invalid email or password' });
} }

View file

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