-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to basics and Redis Cache with user ID
- Loading branch information
Ronak Bokaria
committed
Oct 18, 2020
1 parent
fe44e07
commit 2bf6493
Showing
22 changed files
with
183 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import config from '../../config/env/index.js'; | ||
import APIError from '../services/error.js'; | ||
import User from '../models/User.js'; | ||
const jwt = require('jsonwebtoken'); | ||
const config = require('../../config/env/index.js'); | ||
const APIError = require('../services/error.js'); | ||
const User = require('../models/User.js'); | ||
|
||
export async function checkToken(req, res, next) { | ||
async function checkToken(req, res, next) { | ||
try { | ||
let token = req.headers['x-access-token'] || req.headers['authorization']; | ||
if (token.startsWith('Bearer ')) { | ||
// Remove Bearer from string | ||
// Remove Bearer = require( string | ||
token = token.slice(7, token.length); | ||
} | ||
if (token) { | ||
jwt.verify(token, config.JWT_SECRET, async (err, decoded) => { | ||
if (err) { | ||
apiResponse.expiredAuthResponse(res, 'Token is invalid, Please refresh token or Authenticate again'); | ||
apiResponse.expiredAuthResponse( | ||
res, | ||
'Token is invalid, Please refresh token or Authenticate again', | ||
); | ||
} else { | ||
const user = await User.findById(decoded._id); | ||
req.User = user; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
apiResponse.unauthorizedResponse(res, 'Token Missing, Please check headers or Authenticate to obtain new token'); | ||
apiResponse.unauthorizedResponse( | ||
res, | ||
'Token Missing, Please check headers or Authenticate to obtain new token', | ||
); | ||
} | ||
} catch (err) { | ||
next(new APIError(null, apiResponse.API_STATUS.UNPROCESSABLE_ENTITY, true, err)); | ||
} | ||
} | ||
|
||
export default checkToken; | ||
module.exports = checkToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,54 @@ | ||
export async function pagination(req, res, next) { | ||
let paginationDetails = { | ||
page:0, | ||
limit: 50, | ||
orderBy: 'createdAt', | ||
orderAsc: false, | ||
}; | ||
if(req.query.no_limit) { | ||
paginationDetails = null; | ||
} else { | ||
if(req.query.page){ | ||
paginationDetails.page = parseInt(req.query.page<=1?0:req.query.page-1); | ||
} | ||
if(req.query.limit){ | ||
paginationDetails.limit = parseInt(req.query.limit>0?req.query.limit:50); | ||
} | ||
if(req.query.orderBy){ | ||
paginationDetails.orderBy = req.query.orderBy; | ||
} | ||
if(req.query.orderAsc){ | ||
paginationDetails.orderAsc = req.query.orderAsc==false?-1:1; | ||
} | ||
async function pagination(req, res, next) { | ||
let paginationDetails = { | ||
page: 0, | ||
limit: 50, | ||
orderBy: 'createdAt', | ||
orderAsc: false, | ||
}; | ||
if (req.query.noLimit==1) { | ||
paginationDetails.limit = 99999999; | ||
} else { | ||
if (req.query.page) { | ||
paginationDetails.page = parseInt( | ||
req.query.page <= 1 ? 0 : req.query.page - 1, | ||
); | ||
} | ||
req.pagination = paginationDetails; | ||
res.pagination = paginationDetails; | ||
res.routePath = req.route.path+'::'+req.originalUrl; | ||
req.paginationProcess = function(model) { | ||
try { | ||
if(this.pagination !== null){ | ||
let order = {}; | ||
order[this.pagination.orderBy] = this.pagination.orderAsc; | ||
model.limit(this.pagination.limit) | ||
.skip(this.pagination.limit * this.pagination.page) | ||
.sort(order); | ||
} | ||
} catch(err) { | ||
console.log(err); | ||
} finally { | ||
return model; | ||
} | ||
}; | ||
next(); | ||
if (req.query.limit) { | ||
paginationDetails.limit = parseInt(req.query.limit > 0 ? req.query.limit : 50); | ||
} | ||
} | ||
if (req.query.orderBy) { | ||
paginationDetails.orderBy = req.query.orderBy; | ||
} | ||
if (req.query.orderAsc) { | ||
paginationDetails.orderAsc = req.query.orderAsc == 0 ? -1 : 1; | ||
} | ||
req.pagination = paginationDetails; | ||
res.pagination = paginationDetails; | ||
res.routePath = req.route.path + '::' + req.originalUrl; | ||
let cacheKey = ''; | ||
if (req.User) { | ||
cacheKey = req.User._id + '::'; | ||
} | ||
res.cacheKey = cacheKey + res.routePath; | ||
|
||
req.paginationProcess = function (model) { | ||
try { | ||
if (this.pagination !== null) { | ||
let order = {}; | ||
order[this.pagination.orderBy] = this.pagination.orderAsc; | ||
model | ||
.limit(this.pagination.limit) | ||
.skip(this.pagination.limit * this.pagination.page) | ||
.sort(order); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} finally { | ||
return model; | ||
} | ||
}; | ||
next(); | ||
} | ||
|
||
export default pagination; | ||
module.exports = pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import redis from 'redis'; | ||
import config from '../../config/env/index.js'; | ||
const redis = require('redis'); | ||
const config = require('../../config/env/index.js'); | ||
|
||
const PORT_REDIS = process.env.REDIS_PORT || 6379; | ||
const redisClient = redis.createClient(PORT_REDIS); | ||
|
||
export function setApiCache(key, value) { | ||
exports.setApiCache = function setApiCache(key, value) { | ||
redisClient.set(key, JSON.stringify(value)); | ||
} | ||
}; | ||
|
||
export function getApiCache(req, res, next) { | ||
exports.getApiCache = function getApiCache(req, res, next) { | ||
if (config.REDIS_API_CACHE) { | ||
let key = req.route.path + '::' + req.originalUrl; | ||
redisClient.get(key, (error, data) => { | ||
let cacheKey = ''; | ||
if(req.User){ | ||
cacheKey = req.User._id+'::'; | ||
} | ||
redisClient.get(cacheKey+key, (error, data) => { | ||
if (error) res.status(400).send(error); | ||
if (data !== null) res.status(200).send(JSON.parse(data)); | ||
else next(); | ||
}); | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
export default getApiCache; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.