Compare commits
2 commits
28ccd1fe64
...
8d65c5821a
Author | SHA1 | Date | |
---|---|---|---|
8d65c5821a | |||
d583fb78e8 |
5 changed files with 63 additions and 11 deletions
14
README.md
14
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
|
||||
|
||||
|
@ -219,3 +220,16 @@ status 404
|
|||
"Cart not found."
|
||||
}
|
||||
```
|
||||
|
||||
# Database Schema
|
||||
## User
|
||||
```
|
||||
{
|
||||
name: string,
|
||||
email: string,
|
||||
password: string,
|
||||
cart: {
|
||||
productId: number
|
||||
}
|
||||
}
|
||||
```
|
35
package-lock.json
generated
35
package-lock.json
generated
|
@ -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",
|
||||
|
|
|
@ -14,6 +14,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",
|
||||
|
|
|
@ -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 { firstName, lastName, email, password, address } = req.body;
|
||||
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 (!(email && password && firstName && lastName && address)) {
|
||||
if (!(password && address)) {
|
||||
return res.status(400).json({ error: 'All inputs are required' });
|
||||
}
|
||||
// checkIfUserExists return true if the user exists
|
||||
|
@ -20,8 +26,6 @@ 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,
|
||||
|
@ -43,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' });
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface IUser extends Document {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
address: string;
|
||||
|
@ -35,8 +33,6 @@ 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 },
|
||||
|
|
Loading…
Reference in a new issue