我正在尝试在我的应用程序中使用 session 来跟踪用户登录等。
但是...
当我尝试使用最简单的方法时,我不断收到错误:
Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process.
经过多次搜索,我添加了 firebase-session 功能, 但现在,即使我没有收到错误, session 中也不会保存任何内容。
这是我的代码:
const functions = require('firebase-functions');
const express = require('express');
const session = require('express-session');
const FirebaseStore = require('connect-session-firebase')(session);
const firebase = require('firebase-admin');
const ref = firebase.initializeApp(
functions.config().firebase
);
const app = express();
app.use(session({
store: new FirebaseStore({
database: ref.database()
}),
secret: 'asdfasgbrwewet53hnhwetynsy',
resave: true,
saveUninitialized: true
}));
app.get('/', function(req, res, next) {
console.log(req.session);
req.session.username='rakafa';
res.send('Returning with some text');
});
app.get('/bar', function(req, res, next) {
console.log(req.session);
var someAttribute = req.session.username;
res.send(`This will print the attribute I set earlier: ${someAttribute}`);
});
exports.app = functions.https.onRequest(app);
这是我到目前为止所做的:
- 我最初使用 session 的示例:session-express example
- 我从 npm 获取了 firebase 部分:npm firebase-session
- 尝试使用 radis-store 作为解决方案,但没有说:using radis-store solution
- 使用session-store,给我带来了和不使用一样的磨损.. npm session-store
- 这个问题让我产生了使用 firebase-session database sessions explanations 的想法
- 我看到了这个cookie的例子,但不明白,也没有找到另一个例子cookie session
再说一句,我才刚刚开始学习 Node.js,所以请尝试使用简单的示例,这样我就能理解;)
请原谅我的初学者的错误
请帮忙......
===编辑===
根据orxelm的回答,我将我的ref
更改为:
const ref = firebase.initializeApp({
//admin.credential.cert(serviceAccount)
credential: firebase.credential.applicationDefault(),
databaseURL: "https://*****.firebaseio.com/"
});
但是,当我运行应用程序时,一旦刷新页面, session 中就没有任何内容。
sessions in the server ^^ 我可以看到该应用程序在服务器中创建了很多 session 。 但他们都没有我在应用程序上添加的信息, 由于我只使用一种浏览器,因此应该只有一个 session ,我错了吗?
最佳答案
当我使用 Firebase 托管和功能时,我遇到了这个问题,但只需在 session 中设置 name:"__session"
即可解决。
app.use(session({
name:"__session,
...
添加此内容后,在 session 中保存值(如req.session.username='rakafa'
)工作正常。
关于javascript - 带有 firebase 的 Node.js 不会在 session 中保存信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349708/