使用Firebase进行NodeJS身份验证

Kar*_*cki 25 node.js firebase firebase-authentication firebase-admin

我想通过NodeJS与Firebase进行身份验证并保持会话.客户端无法直接与Firebase通信.

简而言之:

客户端(浏览器)<----> NodeJs(firebase-admin)<----> Firebase

我在NodeJS中创建了Firebase客户端,然后我使用了登录方法:

var firebaseClient = require('firebase');
firebaseClient.initializeApp(config)
firebaseClient.auth().signInWithEmailAndPassword(req.body.email, req.body.password).catch(function(error){
    console.log(error);
})
Run Code Online (Sandbox Code Playgroud)

然后我创建了路由来检查经过身份验证的用户:

app.get('/check',function(req,res){
    var user = firebaseClient.auth().currentUser
    console.log(user)
})
Run Code Online (Sandbox Code Playgroud)

此方法仅允许我保留1个先前登录的用户.

我想使用firebase-admin,但我不知道如何保持会话和验证用户

Hir*_*aka 16

您可以使用客户端SDK在各自的设备/浏览器上对客户端进行身份验证,然后让他们将ID令牌发送到使用firebase-admin(admin SDK)编写的后端服务.admin SDK提供了验证客户发送的ID令牌的方法:https://firebase.google.com/docs/auth/admin/verify-id-tokens

  • 谢谢你的回复:)似乎我找到了另一种选择.我正在尝试使用Google Identity Toolkit的REST服务从云功能中获取IdToken.你认为这是一个可能的解决方案吗?https://firebase.google.com/docs/reference/rest/auth/ (2认同)