Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 代表用户从node.js服务器执行请求_Firebase - Fatal编程技术网

Firebase 代表用户从node.js服务器执行请求

Firebase 代表用户从node.js服务器执行请求,firebase,Firebase,我正在尝试实现一个node.js服务器,其中firebase是我的持久层 用户可以登录并获取从custome authentication firebase模块生成的accessToken 然后,在以后的每个请求中,客户端将向服务器提供其给定的accessToken,然后服务器将通过作为请求用户连接到firebase ref来执行操作 var ref = new Firebase("https://cexample.firebaseio.com/"); ref.authWithC

我正在尝试实现一个node.js服务器,其中firebase是我的持久层

用户可以登录并获取从custome authentication firebase模块生成的accessToken

然后,在以后的每个请求中,客户端将向服务器提供其给定的accessToken,然后服务器将通过作为请求用户连接到firebase ref来执行操作

    var ref = new Firebase("https://cexample.firebaseio.com/");
    ref.authWithCustomToken(token, function(error, authData) {
      ref.child("users").child(authData.uid).set({
        data: authData.uid,
        provider: "custom"
      });

    });
这种方法不起作用,因为firebase只为给定的firebase url维护一次身份验证状态

这意味着执行此代码:

    var ref = new Firebase("https://cexample.firebaseio.com/");
    ref.authWithCustomToken(token1, function(error, authData) {
      ref.child("users").child(authData.uid).set({
        data: authData.uid,
        provider: "custom"
      });

    });

    var ref1 = new Firebase("https://cexample.firebaseio.com/");
    ref1.authWithCustomToken(token2, function(error, authData) {
      ref.child("users").child(authData.uid).set({
        data: authData.uid,
        provider: "custom"
      });

    });
这是行不通的。只调用第二个回调,即ref1的回调


是否能够将node.js服务器向firebase发出的请求作为每个请求的不同用户进行处理?

您可以为要使用的每个单独的身份验证令牌生成一个子进程,但这不是很有效:

// server.js
for (var i in users) {
    var user = users[i];
    var token = createTokenForUser(user);
    child_process.spawn("./single-user-worker.js", [token]);
}

// single-user-worker.js
var token = process.argv.pop();
var ref = new Firebase("https://cexample.firebaseio.com/");
ref.authWithCustomToken(token, ...);
只为服务器创建一个高特权令牌以供所有用户使用会更有效:

var token = createAdminToken();
var ref = new Firebase("https://cexample.firebaseio.com/");
ref.authWithCustomToken(token, function(error, authData) {
  for (var i in users) {
    var user = users[i];
    ref.child("users").child(user.uid).set({
      data: user.uid,
      provider: "custom"
    });
  }
});
node.js服务器是否提供网页服务?客户能否直接联系Firebase?将自定义令牌发送到客户端供其自己使用将是一个更正常的工作流:

function (request, response) {
    var token = createTokenForUser(request.user);
    var script = ['<script>',
        'var token='+JSON.stringify(token)+';',
        'var ref= new Firebase(...);',
        'ref.authWithCustomToken(token, ...);',
        '</script>'].join("\n");
    response.end(script);
}

如果没有标准的Firebase库,将很难对更改做出反应。您需要设置标题
Accept:text/event stream
并为
“data”
事件编写处理程序,并确保在处理完这些事件后关闭连接,这样您就不会增加Firebase账单。

我不想将此逻辑移动到客户端。服务器应负责此操作。如果我对服务器使用特权令牌,那么我将失去firebase为每个经过身份验证的用户提供的角色权限,因为权限中的auth变量始终是服务器的,而不是客户端的。为每个连接的用户生成一个子项效率很低,而且不是有效的解决方案。如果您已设置了指定用户角色和权限的安全规则,则不需要代理。用户可以自己获取数据。请将用例添加到您的问题中,因为您在此处遇到了问题。为了演示这是一个XY问题,您的问题是如何从节点脚本中验证为不同的用户。这个答案对你的问题来说是正确的,尽管它解的是X而不是Y。
var request = require("request");
var token = createTokenForUser(...);
var uid = ...;

request({
    method: "POST",
    url: "https://cexample.firebaseio.com/users/"+uid,
    qs: {auth: token},
    body: {
        data: uid,
        provider: "custom"
    },
    json: true
});