Skip to content

Commit 324a65a

Browse files
committed
jwt integarted
1 parent 86841ce commit 324a65a

File tree

9 files changed

+166
-26
lines changed

9 files changed

+166
-26
lines changed

package-lock.json

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@types/body-parser": "^1.19.0",
1313
"@types/express": "^4.17.8",
1414
"@types/joi": "^14.3.4",
15+
"@types/jsonwebtoken": "^8.5.0",
1516
"@types/mongoose": "^5.7.36",
1617
"@types/node": "^14.14.2",
1718
"@types/nodemailer": "^6.4.0",
@@ -20,6 +21,7 @@
2021
"express": "^4.17.1",
2122
"express-validator": "^6.6.1",
2223
"joi": "^17.3.0",
24+
"jsonwebtoken": "^8.5.1",
2325
"mongoose": "^5.10.10",
2426
"node": "^15.0.1",
2527
"nodemailer": "^6.4.14",

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ npm install --save @types/joi
7171
## To Encrypt the password install node.bcrypt.js
7272

7373
npm i bcrypt
74+
75+
## To install the jsonwebtoken
76+
77+
npm i jsonwebtoken
78+
npm i @types/jsonwebtoken
7479
7580

7681

src/controllers/UserControllers.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import User from '../modals/User';
33
import { NodeMailer } from '../utils/NodeMailer';
44
import { Utils } from '../utils/Utils';
55
import * as Bcrypt from 'bcrypt';
6-
import { nextTick } from 'process';
7-
import { rejects } from 'assert';
6+
import * as Jwt from 'jsonwebtoken';
7+
import { getEnvironmentVariable } from '../environments/env';
88

99
export class UserController {
1010

@@ -17,7 +17,7 @@ export class UserController {
1717

1818
let MAX_TOKEN_TIME = new Utils().MAX_TOKEN_TIME;
1919
try {
20-
const hash = await UserController.encryptPassword(req, res, next);
20+
const hash = await Utils.encryptPassword(req.password);
2121
const data = {
2222
email: email,
2323
password: hash,
@@ -107,26 +107,18 @@ export class UserController {
107107
let d = req.body;
108108
const email = d.email;
109109
const password = d.password;
110+
const user = req.user;
110111

111-
User.findOne({ email: email }).then((user: any) => {
112-
Bcrypt.compare(password, user.password, (err, same) => {
113-
res.send(same)
114-
})
115-
})
116-
}
117-
112+
try {
113+
await Utils.comparePassword({ plainPassword: password, encryptPassword: user.password });
114+
const data = { user_id: req.user._id, email: req.user.email }
115+
const token = Jwt.sign(data, getEnvironmentVariable().jwt_secret, { expiresIn: '120d' });
116+
const response = { user: user, toke: token };
117+
res.json(response)
118+
} catch (e) {
119+
next(e);
120+
}
118121

119-
private static async encryptPassword(req, res, next) {
120-
return new Promise((resolve, reject) => {
121-
Bcrypt.hash(req.body.password, 10, (err, hash) => {
122-
if (err) {
123-
reject(err)
124-
}
125-
else {
126-
resolve(hash)
127-
}
128-
})
129-
})
130122
}
131123

132124
}

src/environments/dev.env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Environment } from './env'
22

33
export const DevEnvironment: Environment = {
4-
db_url: "mongodb+srv://demo:demo@cluster0.cjcjs.mongodb.net/postWeb?retryWrites=true&w=majority"
4+
db_url: "mongodb+srv://demo:demo@cluster0.cjcjs.mongodb.net/postWeb?retryWrites=true&w=majority",
5+
jwt_secret: "devSecret"
56
}

src/environments/env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ProdEnvironment } from "./prod.env";
33

44

55
export interface Environment {
6-
db_url: string
6+
db_url: string;
7+
jwt_secret: string;
78
}
89

910

src/environments/prod.env.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Environment } from './env'
22

33
export const ProdEnvironment: Environment = {
4-
db_url: "mongodb+srv://demo:demo@cluster0.cjcjs.mongodb.net/postWeb?retryWrites=true&w=majority"
4+
db_url: "mongodb+srv://demo:demo@cluster0.cjcjs.mongodb.net/postWeb?retryWrites=true&w=majority",
5+
jwt_secret: "prodSecret"
56
}

src/utils/Utils.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as Bcrypt from 'bcrypt';
2+
13
export class Utils {
24

35
public MAX_TOKEN_TIME = 60000;
@@ -11,6 +13,34 @@ export class Utils {
1113
return parseInt(otp)
1214
}
1315

16+
static encryptPassword(password): Promise<any> {
17+
return new Promise((resolve, reject) => {
18+
Bcrypt.hash(password, 10, (err, hash) => {
19+
if (err) {
20+
reject(err)
21+
}
22+
else {
23+
resolve(hash)
24+
}
25+
})
26+
})
27+
}
28+
29+
static async comparePassword(password: { plainPassword: string, encryptPassword: string }): Promise<any> {
30+
return new Promise((resolve, reject) => {
31+
Bcrypt.compare(password.plainPassword, password.encryptPassword, (err, isValid) => {
32+
if (err) {
33+
reject(new Error(err))
34+
}
35+
else if (!isValid) {
36+
reject(new Error('Email & Password Does Not Match'))
37+
}
38+
else {
39+
resolve({ value: true })
40+
}
41+
})
42+
})
43+
}
44+
}
1445

1546

16-
}

src/validators/UserValidators.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ export class UserValidators {
3434
}
3535

3636
static login() {
37-
return [body('email', 'Email is required').isEmail(),
37+
return [body('email', 'Email is required').isEmail().custom((email, { req }) => {
38+
return User.findOne({ email: email }).then(user => {
39+
if (user) {
40+
req.user = user;
41+
return true;
42+
} else {
43+
throw new Error('User Does Not Exist');
44+
}
45+
})
46+
}),
3847
body('password', 'Password is Required').isAlphanumeric()]
3948
}
4049

0 commit comments

Comments
 (0)