node.js - 使用 firebase admin sdk 进行 REST api 身份验证

标签 node.js rest firebase firebase-authentication firebase-admin

我有一个 REST api,并且使用 jwt token 完成身份验证。为了使 api 更安全(用户和身份验证机制),我想使用 firebase 身份验证。我想知道我们是否可以使用 firebase 作为我的 REST API 的身份验证服务器。

我的理解是客户端应用程序会将用户名和密码发送到 firebase 服务器,并且它们将提供 token 。使用该 token 客户端应用程序将向我们的服务器发送 api 调用。我需要在我的服务器中集成 firebase admin SDK 并使用 admin SDK 验证 token 以从我的数据库获取数据。

如有错误,请指正。

此外,我还担心如何管理刷新 token 以保持我的应用程序登录。

请帮助我以正确的方式集成它,我正在使用 nodejs/expressjs 来创建 API。

最佳答案

can we use firebase as a authentication server for my REST APIs.

是的,这是他们提供的服务之一:https://firebase.google.com/products/auth/

My understanding is that the client app will send the username and password to the firebase server and they will provide a token.

正确。通常的 Firebase 身份验证完全在客户端完成。

但是,如果您需要特定的身份验证机制,例如 LDAP/AD 或某种其他形式的企业恶作剧,那么您需要创建自己的 token ,客户端将使用该 token 进行身份验证:https://firebase.google.com/docs/auth/admin/create-custom-tokens

Using that token client app will send an api call to our server.

正确。客户端成功登录并检索其 ID token 后,您在服务器端需要验证 ID token :https://firebase.google.com/docs/auth/admin/verify-id-tokens通过中间件。

Also, i have a concern that how to manage refresh tokens to keep my app logged in.

只要客户端使用适当的方法来检索 ID token ,您就不必担心这一点。例如,在Web端客户端会调用:https://firebase.google.com/docs/reference/js/firebase.User#getIdToken其中指出(强调我的):

Returns the current token if it has not expired, otherwise this will refresh the token and return a new one.

如您所见,客户端 Firebase SDK 会为您处理一切。您无需在服务器端跟踪 ID token 、刷新 token 或任何其他内容。您所需要做的就是验证 token ,仅此而已。

有关服务器端验证的更多详细信息,请参阅我之前的回答:Firebase authentication using NodeJS

关于node.js - 使用 firebase admin sdk 进行 REST api 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50575569/

相关文章:

IOS Simple rest API 适用于模拟器,但不适用于任何设备

android - Firebase ui - 在 android 中为 FirebaseRecyclerView 设置标题 View

swift - CocoaPods 找不到 pod "FirebaseCore": In snapshot (Podfile. 锁的兼容版本)

node.js - 返回 POST 请求响应后运行方法

java - Spring Boot - Injecting Repository into Controller throws 考虑在您的配置中定义一个类型为 'Type' 的 bean

node.js - Node js 中的 AWS SDK - 从子目录获取对象

mongodb - 如何在 REST API 中管理数据库版本

javascript - 将 Firebase 值插入下拉列表

javascript - 禁用 NestJS 中所有 POST 的状态 201

node.js - 如何 jest.spyOn 仅基类方法,而不是重写的方法