简体   繁体   English

Firebase:如果我使用服务器端 session cookies,如何在客户端获取登录用户的 ID 令牌?

[英]Firebase: How do I get the ID Token for a signed-in user on the client side if I am using server-side session cookies?

I am using Firebase Cloud Functions with Express and Firebase Hosting to serve my multi-page site.我正在使用带有 Express 和 Firebase 托管的 Firebase 云功能来为我的多页站点提供服务。 I have successfully implemented server-side cookies as explained here and as implemented below:我已经成功实现了服务器端 cookies,如此处所述并如下实现:

function verifyLogin(request, response, next) {
    const sessionCookie = request.cookies.__session || '';
    firebase.auth().verifySessionCookie(sessionCookie, true /** checkRevoked */ )
        .then((decodedClaims) => {
            //serve content for user
            return next();
        }).catch(error => {
            // Session cookie is unavailable or invalid. Force user to log in.
            console.log(error);
            response.redirect('/login');
            return;
        });
}

app.get('/', verifyLogin, (request, response) => {
    var page_title = "Home";
    response.render('index', {
        page_title,
    });
});

I am using the Firebase Web SDK (JavaScript) to access the Firebase Cloud Firestore.我正在使用 Firebase Web SDK (JavaScript) 访问 Z035489FF8D092741943E4 Cloud Firestore。 In order to do this, I need to get the idToken on the client side for the currently-logged-in user, like so:为此,我需要在客户端获取当前登录用户的 idToken,如下所示:

firebase.auth().onAuthStateChanged(firebaseUser => {
    if (firebaseUser) {
        firebase.auth().currentUser.getIdTokenResult()
            .then((idTokenResult) => {
                 // Access the Firebase Cloud Firestore here
            });
    }
});

This seems redundant to me, since I already know which user is signed in via my server cookie, but how else can I get the idToken without another call to Firebase Auth?这对我来说似乎是多余的,因为我已经知道哪个用户是通过我的服务器 cookie 登录的,但是如果不再次调用 Firebase Auth,我还能如何获得 idToken?

Is there a way to extrapolate it from my session cookie somehow and pass it to my client from my cloud function as a variable?有没有办法以某种方式从我的 session cookie 中推断出来,并将其从我的云 function 作为变量传递给我的客户端?

Or is there another way to look at this philosophically that I am overlooking?还是有另一种我忽略的哲学方式来看待这个问题?

In the same way you can listen to user sign-in state changes using onAuthStateChanged() , you can also listen to user ID token changes using onIdTokenChanged() .同样,您可以使用onAuthStateChanged()监听用户登录 state 更改,也可以使用onIdTokenChanged () 监听用户 ID 令牌更改。 You will only need to use a new token when the observer you attach shows that a new one appears (for example, when it's refreshed every hour by the SDK).仅当您附加的观察者显示出现新令牌时(例如,当 SDK 每小时刷新一次时),您才需要使用新令牌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在客户端的 XMLHttpRequest 之后,如何在服务器端获取 FormData 值? - How do I get FormData values in Server-side after XMLHttpRequest in client-side? 使用 express 和 mongoose,如何使用 POST 路由将多个 ID 的数组从客户端发送到服务器端? - Using express and mongoose, how do I send an array of multiple IDs from the client-side to server-side using a POST route? 在服务器端使用护照js本地用户身份验证后,如何在客户端使用Cookie来显示用户信息? - How do i use cookies on the client side to display user information after passport js local user auth on the server side? 如何使用HtmlService运行服务器端函数 - How do I run Server-side functions using HtmlService 如何使用 PHP 实际检索服务器端信息,客户端 JavaScript 程序使用 fetch() function 发送给它? - How do I actually retrieve information server-side using PHP that a client-side JavaScript program sent to it with the fetch() function? 如何将服务器端ASP XmlHttpRequest代码转换为客户端JavaScript? - How do I convert my server-side ASP XmlHttpRequest code to client-side JavaScript? 如何在客户端而不是服务器端将我的哈巴狗文件渲染到 html - How do I render my pug files to html on the client-side instead of server-side 如何将数据从客户端表单输入传输到服务器端Nodejs脚本? - How do I transfer data from a client-side form input to a server-side Nodejs script? 如何清除客户端中的服务器端数据表表行 - How do i clear the server-side datatables table rows in client-side 如何在客户端管理当前用户会话? - How do I manage the current user session on the client side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM