我面临的问题是,设置 Firebase session cookie 后,我转到另一个需要安全的端点,但是 req
找不到 cookie。
我正在关注名为 Manage Session Cookies 的 Firebase 教程描述了如何在用户使用 Firebase signInWithEmailAndPassword
登录后生成 cookie功能。用户idToken
是 then
传递给 POST
请求:
首先,我生成一个 token 并将其发送到端点:
function signIn() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
document.getElementById('quickstart-sign-in').disabled = false;
}).then(user => {
// Get the user's ID token as it is needed to exchange for a session cookie.
return user.getIdToken().then(idToken => {
if (firebase.auth().currentUser) {
$.ajax({
method: 'POST',
url: '/login',
data: {'email':firebase.auth().currentUser.email,idToken},
success: function(data) {
//do other things...
}
});
}
});
})
}
URL 端点,正在获取 idToken
从之前的POST
请求,使用 createSessionCookie
创建 session cookie ,然后使用 res.cookie('session', sessionCookie, options)
设置 cookie :
exports.postLogin = (req, res, next) => {
// Get the ID token passed
const idToken = req.body.idToken.toString();
// Set session expiration to 14 days.
const expiresIn = 60 * 60 * 24 * 14 * 1000;
var exampleDB = admin.database().ref('exampleDB');
exampleDB.once('value', function(snapshot) {
//unrelated things happening...
//generate randomData...
}).then(function() {
admin.auth().createSessionCookie(idToken, {expiresIn})
.then((sessionCookie) => {
// Set cookie policy for session cookie.
const options = {maxAge: expiresIn, httpOnly: true, secure: true};
res.cookie('session', sessionCookie, options);
res.status(200).send(randomData).end();
}, error => {
res.status(401).send('UNAUTHORIZED REQUEST!');
});
});
};
当我转到另一个端点 /dashboard
时,我面临的问题从这里开始。 。找不到我应该设置的 cookie,而是收到一条错误消息,指出 TypeError: Cannot read property 'session' of undefined
对于我的 session cookie:
exports.dashboard = (req, res, next) => {
const sessionCookie = req.cookies.session || '';
// a bunch of other code around the session cookie that doesn't even get triggered because of the break in req.cookies.session
}
我是否错误地检索了 cookie?或者我没有正确设置cookie?或者 cookie 是否以某种方式没有被转移到这个新端点 /dashboard
,来自 POST
所在的页面至/login
发生?
记录 req
后至/dashboard
我看到其中有以下内容,但我不知道它是否来自另一个 session 。如果它来自 Firebase,我不知道如何正确访问它:
sessionID: 'ublahxARQVljyGRblahPQrei',
session:
Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
returnTo: '/dashboard',
flash: {},
_csrfSecret: 'r46blahE+kOzblah5==' },
最佳答案
我相信您需要使用__session
cookie。
When using Firebase Hosting together with Cloud Functions or Cloud Run, cookies are generally stripped from incoming requests. source
以前曾以不同的形式遇到过这种情况:Firebase Functions : How to store simple cookies to remember an authenticated user
关于javascript - 无法读取未定义的属性 'session' - 管理 session Cookie Firebase Auth 生成的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039589/