使用Firebase进行身份验证后发布到网址

时间:2016-02-03 21:59:58

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

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

2 个答案:

答案 0 :(得分:1)

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

EntityManagerFactory emf = Persistence.createEntityManagerFactory("testing");

EntityManager em = emf.createEntityManager();

您可以挂钩https://www.firebase.com/docs/web/api/firebase/onauth.html 事件客户端,获取所需信息并将其发送给客户端。

不支持客户端的“帮助”(即firebase自动以某种方式通知您的服务器有关新用户/新登录),不支持(遗憾地)。

编辑:如果你不相信onauth足够安全(并且担心安全问题),那么如何安全地实现同样的目标:

  • 创建集合'onauth-events'。每次客户端收到uid回调时,他们会向某些事件推送onauth-events,并提供必要的信息

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

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

答案 1 :(得分:0)

请参阅以下链接作为参考:

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