Skip to content

Commit 37af973

Browse files
committed
Allow enabling CORS using --allow-cors <origin>
Resolves #64
1 parent 0aeab03 commit 37af973

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

bin/charged

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const args = require('meow')(`
1414
-p, --port <port> http server port [default: 9112]
1515
-i, --host <host> http server listen address [default: 127.0.0.1]
1616
-e, --node-env <env> nodejs environment mode [default: production]
17+
--allow-cors <origin> allow browser CORS requests from <origin> [default: off]
1718
1819
--rate-proxy <uri> proxy to use for fetching exchange rate from bitcoinaverage [default: see proxy-from-env]
1920
--hook-proxy <uri> proxy to use for web hook push requests [default: see proxy-from-env]

src/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const lnPath = process.env.LN_PATH || join(require('os').homedir(), '.lightnin
2626
app.use(require('body-parser').json())
2727
app.use(require('body-parser').urlencoded({ extended: true }))
2828

29+
process.env.ALLOW_CORS && app.use((req, res, next) => {
30+
res.set('Access-Control-Allow-Origin', process.env.ALLOW_CORS)
31+
next()
32+
})
33+
2934
app.get('/info', auth, wrap(async (req, res) => res.send(await ln.getinfo())))
3035

3136
require('./invoicing')(app, payListen, model, auth, lnconf)

src/lib/auth.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const authMiddleware = (name, pass, realm='Lightning Charge') => (req, re
2121

2222
if (!cred || cred.name !== name || cred.pass !== pass)
2323
res.set('WWW-Authenticate', `Basic realm="${realm}"`)
24+
.removeHeader('Access-Control-Allow-Origin')
2425
.sendStatus(401)
2526

2627
else next()

0 commit comments

Comments
 (0)