通过在服务工作者中提取来处理对Firebase数据库的验证 [英] Handling authentification to Firebase Database with Fetch in a Service Worker

查看:161
本文介绍了通过在服务工作者中提取来处理对Firebase数据库的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Fetch API从服务工作人员查询Firebase数据库。然而,它不能正常工作,因为我无法正确认证。



基本上我想要做的是从原始 https://myproject.firebaseapp.com 在服务工作者里面我会这样打电话:

  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();
});

我收到这个错误:

 获取API无法加载https://myproject.firebaseio.com/user/foobar.json。对预检请求的响应不通过访问控制检查:凭据标志为真,但访问控制允许凭证标头为。允许凭证必须是真实的。原因'https://myproject.firebaseapp.com'因此不被允许访问。 

有没有解决这个问题的方法?如何从SW查询/更新Firebase数据库?



我读过 https://jakearchibald.com/2014/using-serviceworker-today/ ,其中一个问题就是这个问题,Fetch请求不发送验证。



理想情况下,能够在SW内部使用Firebase JS API将会非常好,但这似乎不起作用。 Firebase不会将身份验证信息存储为Cookie,也不会将其存储在证书中的任何内容中,因此无需将其发送您的获取请求。相反,您需要从Firebase身份验证中提取令牌:

  firebase.auth()。currentUser.getToken(true) .then(function(token){
// token是你以后需要记住的值
});

获得令牌后,您应该可以将其作为查询参数添加到REST请求,例如?AUTH = {THE_TOKEN} 。这将允许您在服务人员中进行已验证的请求。


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.

Basically what I'm trying to do is from origin https://myproject.firebaseapp.com inside a Service Worker I do a call like this :

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();
});

I'm getting this error :

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.

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

I've read https://jakearchibald.com/2014/using-serviceworker-today/ and one of the gotcha was exactly that problem, the fact that Fetch request do not send authentification.

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.

解决方案

Firebase doesn't store authentication info as a cookie or in anything that would be sent along in the credentials, so there's no need to send them in your fetch request. Instead, you'll need to pull the token from Firebase Auth:

firebase.auth().currentUser.getToken(true).then(function(token) {
  // token is the value you'll need to remember for later
});

Once you've got the token, you should be able to add it as a query parameter to the REST request e.g. ?auth={THE_TOKEN}. This will allow you to make your authenticated request in the Service Worker.

这篇关于通过在服务工作者中提取来处理对Firebase数据库的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆