Firebase Admin SDK-当前登录用户-Node.js [英] Firebase Admin SDK - Current Logged In User - Nodejs

查看:100
本文介绍了Firebase Admin SDK-当前登录用户-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在firebase admin SDK中获取currentUser.在客户端SDK中,我们可以使用

How do i get currentUser in firebase admin SDK. In client SDK we can use

onAuthStateChanged() or firebase.auth().currentUser()

在NodeJs Server.js文件firebase管理员SDK中 onAuthStateChanged()不起作用,这里应该做什么?

In NodeJs Server.js file firebase admin SDK onAuthStateChanged() Do not work, what should work here?

基本上,我只希望登录的用户访问我的innerPages,所以我正在使用express,nodejs app.get 方法创建路由.

Basically i want only logged in users to access my innerPages so i am using express,nodejs app.get method to create routes.

app.get('/', authenticationRequired ,function(req,res){
    res.sendFile(path.join(__dirname+'/index.html'));
});

并尝试编写 authenticationRequired 方法(根据用户是否登录返回false或true).

And trying to write authenticationRequired method (returns false or true depending on if user is logged in).

推荐答案

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.

这篇关于Firebase Admin SDK-当前登录用户-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆