Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abdulmuneer22/5e86f41547f3ebcc11598a3d06e078a1 to your computer and use it in GitHub Desktop.
Save abdulmuneer22/5e86f41547f3ebcc11598a3d06e078a1 to your computer and use it in GitHub Desktop.
'use strict';
// proxy configurator on apigateway
var httpProxy = require('http-proxy');
// Create reverse proxy instance
var proxy = httpProxy.createProxy();
function forwardToInternalPath(req, res) {
// a match method is exposed on the proxy rules instance to test a request to
// see if it matches against one of the specified rules
var target;
target_base = 'http://127.0.0.1:3002/api/'
target_end_point_user_service = '/user-service/login'
//you can create multiple targets like this
//extract targetted service from url , use any regx for that
if (req.url === target_end_point_user_service) {
var access_token = req.header.access_token // you need to check how it is being passed
var userId = req.header.userId
//Find access token for the userId from User/Customer Collection
if(Response.access_token === access_token){
//User has passed correct access token
return proxy.web(req, res, {
target: target,
});
}else{
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Invalid access token!!');
}
}
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Invalid request!!');
}
module.exports = function enableAuthentication(server) {
server.use(function(req, res, next) {
if (
req.path
) {
forwardToInternalPath(req, res);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment