Node.js 使用firebase身份验证在服务器端登录,但不在客户端登录
我正在服务器端使用带有express的节点,并尝试使用Firebase Auth从客户端登录新用户,问题是当我将令牌ID发送到服务器,然后重定向到仪表板时,客户端看不到用户已登录 我的客户端代码:Node.js 使用firebase身份验证在服务器端登录,但不在客户端登录,node.js,firebase,express,firebase-authentication,Node.js,Firebase,Express,Firebase Authentication,我正在服务器端使用带有express的节点,并尝试使用Firebase Auth从客户端登录新用户,问题是当我将令牌ID发送到服务器,然后重定向到仪表板时,客户端看不到用户已登录 我的客户端代码: firebase .auth() .createUserWithEmailAndPassword(login, password) .then((res) => { return user.getIdToken().then(
firebase
.auth()
.createUserWithEmailAndPassword(login, password)
.then((res) => {
return user.getIdToken().then((idToken) => {
return fetch("/sessionLogin", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"CSRF-Token": Cookies.get("XSRF-TOKEN"),
},
body: JSON.stringify({ idToken }),
});
});
})
.catch(function(error) {
console.log("Login Failed!", error);
window.alert(error.message);
})
.then(() => {
window.location.assign("/success");
});
return false;
});
服务器端:
app.post("/sessionLogin", (req, res) => {
const idToken =req.body.idToken.toString();
const expiresIn = 60 * 60 * 24 * 2 * 1000;
admin
.auth()
.createSessionCookie(idToken, { expiresIn })
.then(
(sessionCookie) =>{
const options = { maxAge: expiresIn, httpOnly: true};
res.cookie("session", sessionCookie, options);
res.end(JSON.stringify({ status: "success"}));
},
(error) => {
res.status(401).send("UNAUTHORIZED REQUEST");
}
);
});
app.get("/sessionLogout", (req, res) => {
res.clearCookie("session");
res.redirect("/");
})
然后在检查用户是否在客户端使用
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log(user)
console.log("user in")
} else {
console.log(user)
console.log("user out")
}
});
它告诉我用户未登录且为空。我试图找到这个问题的答案,但通常情况下是另一种方式——从客户端向服务器发送令牌