【问题标题】:Post to URL after authenticating with Firebase使用 Firebase 进行身份验证后发布到 URL
【发布时间】:2016-05-13 07:57:38
【问题描述】:

我想知道在用户使用电子邮件和密码通过 firebase 进行身份验证后,是否有办法将身份验证令牌发布到我自己的服务器。我想在我的服务器上的辅助数据库中存储一些信息,如果他们已经通过 Firebase 进行身份验证,他们只能访问这些信息。这可能吗?

【问题讨论】:

  • 确实如此。您使用什么语言?你有没有尝试过什么?如果是这样,请点击下方的小edit 链接,将您尝试过的代码添加到您的问题中。
  • 我什么都没试过,但我在服务器上使用 node.js,在客户端使用 ionic 应用程序。

标签: node.js ionic-framework firebase firebase-authentication


【解决方案1】:

请参阅下面的链接作为参考:

https://firebase.google.com/docs/functions/callable#java

在 Firebase 中设置包含您所需代码的云函数(在您的情况下,云函数将被编写为您的应用和辅助数据库之间的代理)。

在从您的应用调用触发云功能的代码之前,请在传递给云功能的数据中设置一些关于用户的参数:

// Message text passed from the client.
const text = data.text;
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

如果用户未在您的应用中进行身份验证,则发送到云函数的数据会将与用户相关的数据设置为 null。然后,在您的云功能中,您可以检查用户是否已通过身份验证(请参见下面的代码)。此时,您可以通过云功能将原始数据从您的应用程序传递到您的辅助数据库。

// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');
}

【讨论】:

    【解决方案2】:

    这可能就是你要找的东西:

    https://www.firebase.com/docs/web/api/firebase/onauth.html
    

    您可以在客户端连接到onauth,获取您需要的信息并将其发送给客户端。

    不支持在没有客户端“帮助”的情况下执行此操作(即,firebase 会自动以某种方式通知您的服务器有关新用户/新登录的信息),不支持(遗憾)。

    编辑:如果您不相信 uids 足够安全(安全是问题),这里是如何安全地实现相同目标的方法:

    • 创建集合“onauth-events”。每次客户端收到onauth 回调时,他们都会向 onauth-events 推送一些包含任何必要信息的对象

    • 您可以轻松设置验证规则,使被推送到onauth_events 的对象中的uid 必须与用户的真实uid 匹配

    • 您的服务器可以监听 onauth-events 集合中的 child_added 事件并做出相应反应。

    【讨论】:

    • 我担心这种方法存在潜在的安全问题。如果客户端接收到 uid 和令牌并将它们发送到服务器以用作身份验证,那么阻止他们获取任何人的 uid 并将其令牌设置为“bobTheBuilder”然后弄乱他们的信息?有没有办法让这更安全?
    • 你说得对,我不确定这些 uid 有多少是不可猜测的。无论如何,我用另一种你可以使用的方法编辑了帖子。
    猜你喜欢
    • 2019-08-31
    • 2018-09-03
    • 1970-01-01
    • 2020-10-09
    • 2017-03-11
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多