Handling authentification to Firebase Database with Fetch in a Service Worker
<p>I'm trying to query a Firebase database from a Service Worker using the Fetch API. However it doesn't work as expected as I can't get authenticated correctly.</p>

<p>Basically what I'm trying to do is from origin <a href="https://myproject.firebaseapp.com" rel="nofollow">https://myproject.firebaseapp.com</a> inside a Service Worker I do a call like this :</p>

<pre><code>var fetchOptions = {};
fetchOptions.credentials = 'include';
var url = options.messageUrl;
var request = new Request('https://myproject.firebaseio.com/user/foobar.json', fetchOptions);   
messagePromise = fetch(request).then(function(response) {
  return response.json();
});
</code></pre>

<p>I'm getting this error :</p>

<pre><code>Fetch API cannot load https://myproject.firebaseio.com/user/foobar.json. Response to preflight request doesn't pass access control check: Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'https://myproject.firebaseapp.com' is therefore not allowed access.
</code></pre>

<p>Any idea of a way to fix it? How one should do to query/update the Firebase database from a SW?</p>

<p>I've read <a href="https://jakearchibald.com/2014/using-serviceworker-today/" rel="nofollow">https://jakearchibald.com/2014/using-serviceworker-today/</a> and one of the gotcha was exactly that problem, the fact that Fetch request do not send authentification.</p>

<p>Ideally it would be great to be able to use the Firebase JS API inside a SW but this doesn't seem to work as well.</p>