gpt4 book ai didi

javascript - 如何检查用户是否在 Firebase 和 Express/Node.js 中进行了身份验证?

转载 作者:搜寻专家 更新时间:2023-10-31 23:34:08 25 4
gpt4 key购买 nike

如果我有一个页面只能由经过身份验证的用户访问,我该如何检查用户是否经过身份验证?

我尝试使用 (firebase.auth().currentUser !== null) 但我收到一条错误消息:TypeError: firebase.auth is not a function

我有以下配置:

const express = require('express'),
firebase = require('firebase'),
app = express();

app.use(express.static("/public"));

var config = {
apiKey: "xxxx",
authDomain: "xxxx",
databaseURL: "xxxx",
projectId: "xxxx",
storageBucket: "xxxx",
messagingSenderId: "xxxx"
};

firebase.initializeApp(config);

app.get("/dashboard", (request, response) => {
if (firebase.auth().currentUser !== null){
response.render("dashboard.ejs")
}
else{
response.render("login.ejs");
}
});

最佳答案

您的代码位于 Express 应用程序中,这意味着它在服务器上运行。您使用的 Firebase SDK 适用于客户端设备,无法在您的 Express 环境中正常运行。服务器上没有“当前用户”的概念。当然,每个请求都可以将用户 ID 传递给服务器,但服务器本身是无状态的。

在您的 Express 服务器中,您需要使用 Firebase Admin SDK。看看这个Cloud Functions sample on how to build an authenticated endpoint .

关于javascript - 如何检查用户是否在 Firebase 和 Express/Node.js 中进行了身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50643768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com