Skip to content

Commit eb83c07

Browse files
committed
routes protecting
1 parent 324a65a commit eb83c07

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

src/controllers/UserControllers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class UserController {
4747
static async verify(req, res, next) {
4848

4949
const verificationToken = req.body.verification_token;
50-
const email = req.body.email;
50+
const email = req.user.email;
5151

5252
try {
5353
const user = await User.findOneAndUpdate({
@@ -73,7 +73,7 @@ export class UserController {
7373

7474
static async resendVerificationEmail(req, res, next) {
7575
// console.log(re)
76-
const email = req.query.email;
76+
const email = req.user.email;
7777
const verificationToken = Utils.generateVerificationToken();
7878
try {
7979
const user = await User.findOneAndUpdate({ email: email }, {
@@ -83,6 +83,7 @@ export class UserController {
8383

8484
if (user) {
8585
//SEND VERIFICATION EMAIL
86+
console.log("verification code:", verificationToken)
8687
await NodeMailer.sendEmail({
8788
to: ['rahulgbu13@gmail.com'],
8889
subject: 'Email Verification',

src/middleware/CheckError.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
import { validationResult } from "express-validator";
2+
import * as Jwt from 'jsonwebtoken';
3+
import { getEnvironmentVariable } from "../environments/env";
24

35
export class GlobalCheckErrorMiddleWare {
46

57
static checkError(req, res, next) {
6-
7-
const error = validationResult(req);
8-
8+
const error = validationResult(req);
99
if (!error.isEmpty()) {
1010
next(new Error(error.array()[0].msg))
1111
} else {
1212
next();
1313
}
14-
1514
}
1615

16+
static async authentication(req, res, next) {
17+
const authHeader = req.headers.authorization;
18+
const token = authHeader ? authHeader.slice(7, authHeader.length) : null;
19+
20+
try {
21+
req.errorStatus = 401;
22+
Jwt.verify(token, getEnvironmentVariable().jwt_secret, (err, decoded) => {
23+
24+
if(err){
25+
next(err)
26+
}
27+
else if(!decoded){
28+
next(new Error('User Not Authorised'))
29+
}
30+
else{
31+
req.user = decoded;
32+
next();
33+
}
34+
35+
})
36+
}
37+
catch (e) {
1738

39+
}
40+
41+
}
1842

1943
}

src/routers/UserRouter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class UserRouter {
1919

2020

2121
getRoutes() {
22-
this.router.get('/send/verification/email', UserValidators.resendVerificationEmail(), UserController.resendVerificationEmail);
22+
this.router.get('/send/verification/email', UserValidators.resendVerificationEmail(),GlobalCheckErrorMiddleWare.authentication, UserController.resendVerificationEmail);
2323

2424

2525
}
@@ -29,7 +29,7 @@ class UserRouter {
2929
}
3030
patchRoutes() {
3131

32-
this.router.patch('/signup', UserValidators.verifyUser(), GlobalCheckErrorMiddleWare.checkError, UserController.verify);
32+
this.router.patch('/verify', UserValidators.verifyUser(), GlobalCheckErrorMiddleWare.checkError,GlobalCheckErrorMiddleWare.authentication, UserController.verify);
3333
}
3434
deleteRoutes() {
3535

src/validators/UserValidators.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class UserValidators {
2525
static verifyUser() {
2626
return [
2727
body('verification_token', 'Verifiction Token is Required').isNumeric(),
28-
body('email', 'Email is required').isEmail()
2928
]
3029
}
3130

0 commit comments

Comments
 (0)