检查用户是否已从 Node.js(云功能)登录 Firebase Auth

Ker*_*omo 4 node.js firebase typescript firebase-authentication google-cloud-functions

我正在尝试在 Node.js 上创建 Express 动态网页。我想在服务器(Firebase Cloud Functions)的路径上执行以下逻辑/

  • 如果客户端已登录(Firebase Auth),则呈现主页my_home_page.html
  • 如果客户端未登录,则渲染登录页面my_login_page.html

所以我需要实现这个isClientLoggedIn功能:

import * as functions from 'firebase-functions';
import * as express from 'express';

const app = express();

export const redirects = functions.https.onRequest(app);

app.set('views', './res');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

function isClientLoggedIn(request: Request): bool {
    return // ???
}
app.get('/', (req, res) => {
    if (isClientLoggedIn(req)) {
        res.render('my_home_page.html');
    } else {
        res.render('my_login_page.html');
    }
});
Run Code Online (Sandbox Code Playgroud)

不过我查了Firebase Auth文档,不知道如何实现这个功能。是否可以从 HTTP 请求检查客户端的登录状态,或者是否有其他方法来呈现正确的内容?

Fra*_*len 5

HTTPS 触发的 Cloud Functions 中没有内置方法来了解请求是否来自已登录的用户。如果您希望能够确定这一点,则需要传递用户的 ID 令牌以及请求,并在 Cloud Function 中解码并验证该令牌。有关此方面的简单示例,请参阅存储库中的经过身份验证的 JSON APIfunction-samples