4

I am trying to use session in my app, to keep track on user log-in and more.

But...

When I tried using the simplest way, I kept getting the error :

Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process.

There for, after much searching, I added the firebase-session functionality, but now, even though I get no error, nothing is saved in the session.

This is my code:

    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);

This is what I did so far:

Just another word, I only just started learning Node.js, so please try using simple examples, so I would understand ;)

And please forgive my starte's mistakes

Please, HELP....

=== edit ===

According to orxelm's answer, I changed my ref to this:

    const ref = firebase.initializeApp({
      //admin.credential.cert(serviceAccount)
      credential: firebase.credential.applicationDefault(),
      databaseURL: "https://*****.firebaseio.com/"
    });

But still, when I run the app, there is nothing in the session once I refresh the page.

sessions in the server ^^ I can see that the app created a lot of sessions in the server. but none of them have the information I added on the app, and since I use only one browser, there should only one session, am I wrong?

Rakkefet
  • 73
  • 8

2 Answers2

1

I had this problem, when I was using firebase hosting and functions, but can be solved by simply setting name:"__session" in the session.

app.use(session({
  name:"__session,
  ...

after adding this, saving values in the session like req.session.username='rakafa' worked fine.

Abraham
  • 12,140
  • 4
  • 56
  • 92
0

Where is you firebase config? Maybe try to initialize the firebase admin this way:

 admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
 });
orxelm
  • 1,114
  • 14
  • 17