Skip to content

Commit

Permalink
bg(security): protect all routes
Browse files Browse the repository at this point in the history
- refactor the token verification middlewares
- [Finishes #170799329]
  • Loading branch information
NiyongaboEric committed Jan 20, 2020
1 parent a600981 commit b0d68c4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
12 changes: 12 additions & 0 deletions src/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ class UserController {
return Response.errorMessage(req, res, 'Signout failed', 500);
}
}

/**
* Token is not blacklisted
* @description GET /api/v1/token/valid
* @static
* @param {object} req request object
* @param {object} res response object
* @returns {object} Logout
*/
static async isBlackListed(req, res) {
return Response.successMessage(req, res, 'Token is valid', true, HttpStatus.OK);
}
}

export default UserController;
34 changes: 0 additions & 34 deletions src/middlewares/advancedVerifyToken.js

This file was deleted.

9 changes: 8 additions & 1 deletion src/middlewares/verifyToken.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import jwt from 'jsonwebtoken';
import Response from '../helpers/Response';
import UserService from '../services/UserService';

/**
* verify token
* @param {object} req request object
Expand All @@ -15,11 +17,16 @@ const verifyToken = (req, res, next) => {
}
jwt.verify(
token, process.env.JWT_KEY,
(err, result) => {
async (err, result) => {
if (err) {
return Response.errorMessage(req, res, err, 401);
}
const isTokenExist = await UserService.blacklistToken(token);
if (isTokenExist) {
return Response.errorMessage(req, res, 'You have provided an invalid token', 401);
}
req.user = result;
result.token = token;
next();
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/userRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import UserController from '../../controllers/UserController';
import verifyToken from '../../middlewares/verifyToken';
import checkInputDataError from '../../middlewares/checkInputDataError';
import customValidator from '../../middlewares/customValidator';
import advancedVerifyToken from '../../middlewares/advancedVerifyToken';

const userRoute = express.Router();

Expand Down Expand Up @@ -152,5 +151,6 @@ const { isImage } = customValidator;
userRoute.put('/profile-settings', verifyToken, profileUpdateRules(), checkInputDataError, isImage, isManager, isUserVerified, UserController.updateProfile);
userRoute.get('/view-profile', verifyToken, isUserVerified, UserController.viewProfile);
userRoute.get('/notification', verifyToken, isUserVerified, UserController.viewNotification);
userRoute.patch('/logout', advancedVerifyToken, UserController.logout);
userRoute.patch('/logout', verifyToken, UserController.logout);
userRoute.get('/token/valid', verifyToken, UserController.isBlackListed);
export default userRoute;
4 changes: 2 additions & 2 deletions src/tests/mock/tripMockData.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ const tripMockData = {
{
originId: 3,
destinationId: 5,
startDate: '2020-01-20',
returnDate: '2021-01-10',
startDate: '2020-05-20',
returnDate: '2021-05-10',
reason: 'edit me multicity 1'
},
{
Expand Down

0 comments on commit b0d68c4

Please sign in to comment.