[英]How to distinguish an authenticated user at client side with Passport and node.js?
[英]How to check if user is authenticated in Firebase and Express/Node.js?
如果我的頁面只能由經過身份驗證的用戶訪問,如何檢查用戶是否經過身份驗證?
我嘗試使用(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環境中無法正常使用。 服務器上沒有“當前用戶”的概念。 當然,可以將每個請求的用戶標識傳遞給服務器,但是服務器本身是無狀態的。
在您的Express服務器中,您將要使用Firebase Admin SDK。 查看此Cloud Functions示例,了解如何構建經過身份驗證的端點 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.