Skip to content

Commit 421b2b9

Browse files
rexxarsbjoerge
authored andcommitted
[client] Use uncredentialed requests unless withCredentials or token is specified (#114)
1 parent c346e0b commit 421b2b9

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

packages/@sanity/client/src/data/listen.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@ module.exports = function listen(query, params, opts = {}) {
1717
const options = defaults(opts, defaultOptions)
1818
const listenOpts = pick(options, possibleOptions)
1919
const qs = encodeQueryString({query, params, options: listenOpts})
20-
const {url, token} = this.clientConfig
21-
22-
const authHeaders = {}
23-
if (token) {
24-
authHeaders.Authorization = `Bearer ${token}`
25-
}
20+
const {url, token, withCredentials} = this.clientConfig
2621

2722
const uri = `${url}${this.getDataUrl('listen', qs)}`
2823
const listenFor = options.events ? options.events : ['mutation']
2924
const shouldEmitReconnect = listenFor.indexOf('reconnect') !== -1
3025

26+
const esOptions = {}
27+
if (token || withCredentials) {
28+
esOptions.withCredentials = true
29+
}
30+
31+
if (token) {
32+
esOptions.headers = {
33+
Authorization: `Bearer ${token}`
34+
}
35+
}
36+
3137
return new Observable(observer => {
32-
const es = new EventSource(uri, assign(
33-
{withCredentials: true},
34-
token ? {headers: authHeaders} : {}
35-
))
38+
const es = new EventSource(uri, esOptions)
3639

3740
es.addEventListener('error', onError, false)
3841
es.addEventListener('channelError', onChannelError, false)

packages/@sanity/client/src/http/requestOptions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = config => {
1414
return {
1515
headers: headers,
1616
timeout: ('timeout' in config) ? config.timeout : 30000,
17-
withCredentials: config.withCredentials !== false,
18-
json: true
17+
json: true,
18+
withCredentials: Boolean(config.token || config.withCredentials)
1919
}
2020
}

0 commit comments

Comments
 (0)