使用 Firebase 进行 NodeJS 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43438981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:09:58  来源:igfitidea点击:

NodeJS authentication with Firebase

node.jsfirebasefirebase-authenticationfirebase-admin

提问by Karol Bilicki

I would like to authenticate and keep sessions via NodeJS with Firebase. Client can't directly communicate with Firebase.

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

In short:

简而言之:

Client (browser) <----> NodeJs(firebase-admin) <----> Firebase

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

I created Firebase client in NodeJS, then I used login method:

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

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

and then I created route to check authenticated user:

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

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

This method only allows me keep 1 previously logged user.

这种方法只允许我保留 1 个以前登录的用户。

I would like to use firebase-admin, but I don't know how to keep session and authenticate users

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

回答by Hiranya Jayathilaka

You can authenticate clients on their respective devices/browsers using the client SDK, and them get them to send an ID token to a backend service written using firebase-admin (the admin SDK). The admin SDK provides methods for validating ID tokens sent by clients: https://firebase.google.com/docs/auth/admin/verify-id-tokens

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