使用 Firebase 进行身份验证后 Post 到 URL

Post to URL after authenticating with Firebase

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

这可能是您要找的:

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

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

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

编辑:如果您认为 uid 不够安全(并且安全是关注点),以下是如何安全地实现相同目的的方法:

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

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

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

参考下面的link:

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;

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

// 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.');
}