diff --git a/README.md b/README.md index 7128185..a9718c6 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,19 @@ http://localhost:8010/proxy/movies/list End result will be a request to `https://www.yourdomain.ie/movies/list` without the CORS issues! +If you are proxying a location that requires credentials you'll find that you cannot allow origin * and send credentials. +To resolve this you can pass the specific origin you want to allow: + +``` +lcp --proxyUrl https://www.yourdomain.ie --allowedOrigin http://localhost:3000 +``` + +Also when dealing with passing credentials, you may need to enable sending the include credentials header. +``` +lcp --proxyUrl https://www.yourdomain.ie --allowedOrigin http://localhost:3000 --includeCredentials +``` + + Alternatively you can install the package locally and add a script to your project: ```json @@ -51,3 +64,5 @@ Alternatively you can install the package locally and add a script to your proje | --proxyUrl | https://www.google.ie | | | --proxyPartial | foo | proxy | | --port | 8010 | 8010 | +| --allowedOrigin | http://localhost:3000 | * | +| --includeCredentials | true | true | diff --git a/bin/lcp.js b/bin/lcp.js index e2286ae..6cea0ee 100644 --- a/bin/lcp.js +++ b/bin/lcp.js @@ -10,7 +10,17 @@ var optionDefinitions = [ type: String, defaultValue: '/proxy' }, - { name: 'proxyUrl', type: String } + { name: 'proxyUrl', type: String }, + { + name: 'allowedOrigin', + type: String, + defaultValue: '*' + }, + { + name: 'includeCredentials', + type: Boolean, + defaultValue: false + } ]; try { @@ -18,7 +28,7 @@ try { if (!options.proxyUrl) { throw new Error('--proxyUrl is required'); } - lcp.startProxy(options.port, options.proxyUrl, options.proxyPartial); + lcp.startProxy(options.port, options.proxyUrl, options.proxyPartial, options.allowedOrigin, options.includeCredentials); } catch (error) { console.error(error); } diff --git a/lib/index.js b/lib/index.js index d3b87a2..4838998 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,8 +4,15 @@ var cors = require('cors'); var chalk = require('chalk'); var proxy = express(); -var startProxy = function(port, proxyUrl, proxyPartial) { - proxy.use(cors()); + +var startProxy = function(port, proxyUrl, proxyPartial, allowedOrigin, includeCredentials) { + + var corsOptions = { + origin: allowedOrigin, + credentials: includeCredentials + } + + proxy.use(cors(corsOptions)); proxy.options('*', cors()); // remove trailing slash @@ -26,7 +33,9 @@ var startProxy = function(port, proxyUrl, proxyPartial) { console.log(chalk.bgGreen.black.bold.underline('\n Proxy Active \n')); console.log(chalk.blue('Proxy Url: ' + chalk.green(cleanProxyUrl))); console.log(chalk.blue('Proxy Partial: ' + chalk.green(cleanProxyPartial))); - console.log(chalk.blue('PORT: ' + chalk.green(port) + '\n')); + console.log(chalk.blue('PORT: ' + chalk.green(port))); + console.log(chalk.blue('Allowed Origin: ' + chalk.green(allowedOrigin))); + console.log(chalk.blue('Include Credentials: ' + chalk.green(includeCredentials) + '\n')); console.log( chalk.cyan( 'To start using the proxy simply replace the proxied part of your url with: ' + diff --git a/package.json b/package.json index dec28c4..8b67f3b 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,13 @@ "type": "git", "url": "git+https://github.com/garmeeh/local-cors-proxy.git" }, - "keywords": ["cors", "proxy", "simple", "node", "express"], + "keywords": [ + "cors", + "proxy", + "simple", + "node", + "express" + ], "bugs": { "url": "https://github.com/garmeeh/local-cors-proxy/issues" },