Skip to content

Commit e798cd1

Browse files
committed
Merge pull request #4 from leroix/feature/master/allow-CORS-cookies
Feature/master/allow cors cookies
2 parents 454fe6f + 709c523 commit e798cd1

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ nanoajax.ajax({url: '/some-post-url', method: 'POST', body: 'post=content&args=y
5959
- `method` `"GET", "POST", "PUT", etc`
6060
- `body` string body (if its not url-encoded, make sure to set `Content-Type` header)
6161
- `headers` header object
62+
- `withCredentials` `true or false` only applicable to CORS (does not work in IE)
6263

6364
## Compatibility
6465

index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ exports.ajax = function (params, callback) {
33
var headers = params.headers || {}
44
, body = params.body
55
, method = params.method || (body ? 'POST' : 'GET')
6+
, withCredentials = params.withCredentials || false
67

78
var req = getRequest()
89

10+
// has no effect in IE
11+
// has no effect for same-origin requests
12+
// has no effect in CORS if user has disabled 3rd party cookies
13+
req.withCredentials = withCredentials
14+
915
req.onreadystatechange = function () {
1016
if (req.readyState == 4)
1117
callback(req.status, req.responseText, req)
@@ -34,4 +40,4 @@ function getRequest() {
3440

3541
function setDefault(obj, key, value) {
3642
obj[key] = obj[key] || value
37-
}
43+
}

nanoajax.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {},
1010
"devDependencies": {
1111
"body-parser": "^1.6.2",
12+
"cookie-parser": "^1.3.4",
1213
"express": "^4.8.2",
1314
"localtunnel": "^1.5.0",
1415
"morgan": "^1.2.2"

test/index.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,22 @@ function defineTests(ajax) {
7474
done()
7575
})
7676
})
77+
78+
test('withCredentials', function (done) {
79+
ajax('/cors-url', function (code, body) {
80+
ajax({
81+
url: body + '/cookie-setter'
82+
, withCredentials: true
83+
}, function (code, cookieValue) {
84+
ajax({
85+
url: body + '/cookie-verifier?cookie_value=' + cookieValue
86+
, withCredentials: true
87+
}, function (code) {
88+
assert.equal(code, 200, 'Server could not verify cookie value')
89+
done()
90+
})
91+
})
92+
})
93+
})
7794
}
78-
}
95+
}

test/server.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var tunnel = localtunnel(process.env.ZUUL_PORT, function (err, tunnel) {
1111
app.use(require('morgan')('dev'))
1212
app.use(require('body-parser').urlencoded({extended:false}))
1313
app.use(require('body-parser').json())
14+
app.use(require('cookie-parser')())
1415

1516
app.get('/nanoajax.min.js', function (req, res) {
1617
fs.createReadStream(__dirname + '/../nanoajax.min.js', {encoding:'utf8'})
@@ -49,4 +50,23 @@ app.get('/header', function (req, res) {
4950
res.send(req.headers['x-custom'])
5051
})
5152

52-
app.listen(process.env.ZUUL_PORT)
53+
app.get('/cookie-setter', function (req, res) {
54+
var randomNumber = Math.random() + ''
55+
res.cookie('doge', randomNumber)
56+
res.setHeader('Access-Control-Allow-Origin', req.get('origin'))
57+
res.setHeader('Access-Control-Allow-Credentials', true)
58+
res.send(randomNumber)
59+
})
60+
61+
app.get('/cookie-verifier', function (req, res) {
62+
res.setHeader('Access-Control-Allow-Origin', req.get('origin'))
63+
res.setHeader('Access-Control-Allow-Credentials', true)
64+
65+
if (req.query.cookie_value !== req.cookies.doge) {
66+
res.status(500).send('Could not verify cookie')
67+
} else {
68+
res.send('OK')
69+
}
70+
})
71+
72+
app.listen(process.env.ZUUL_PORT)

0 commit comments

Comments
 (0)