且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase Admin SDK-当前登录用户-Node.js

更新时间:2023-12-01 16:05:10

Firebase Admin SDK本身并不知道什么用户正在使用您的Express Web应用程序.您将需要为此实现代码.如果您已经在Web应用程序中使用Firebase身份验证,请执行以下步骤:

The Firebase Admin SDK does not inherently know what user is using your Express web app. You will need to implement code for this. If you're already using Firebase Authentication in the web app, this takes these steps:

  1. 在客户端上获取用户的ID令牌

您可以使用firebase.auth().currentUser.getIdToken()方法获取用户的ID令牌.

You can get a user's ID token by using the firebase.auth().currentUser.getIdToken() method.

将此令牌从客户端传递到服务器

Pass this token from the client to the server

在服务器上验证令牌

可以使用admin.auth().verifyIdToken(idToken)方法验证用户ID令牌.成功后,它将返回decodedToken的诺言,否则将返回被拒绝的诺言.

The user ID token can be verified using the admin.auth().verifyIdToken(idToken) method. On success it will return a promise with the decodedToken, otherwise it will return a rejected promise.