12

I'm trying to just remeber a user returning to the site and count views, only after 5 minutes. I did this, works when using Firebase Serve, but the cookies are not being stored after deploy.

Somewhere up in the app.

app.use(cookieSession({ name: 'session', keys: ['utl__key_s1', 'utl__key_s2'] }));

Trying to check if session exists and isn't more than 5 min old.

function sessionExists(req) {
    const t = req.session.viewTime;

    if (t == null) {
        req.session.viewTime = + new Date();
        return false;
    }

    const fiveMinutes = ((1000) * 60) * 5;
    if (((+new Date()) - t) > fiveMinutes) {
        req.session = null;
        return false;
    }

    return true;
}

Then I find out the issue is that we have to use __session. That I don't really understand. Can I get an example with context to the above code examples?

Relm
  • 7,923
  • 18
  • 66
  • 113
  • You're aware that Firebase has an authentication system? – Dominic Apr 09 '18 at 08:20
  • 2
    @DominicTobias I'm not trying to auth a user, I want to remember any user that visits the page, authed or not, simple as that. – Relm Apr 09 '18 at 08:23
  • In [this issue](https://github.com/expressjs/session/issues/505) it has been suggested to change the name of `session` to `__session` like so `app.use(cookieSession({ ... name: '__session' }));` but no one has mention what to do next. – Amr Aly Apr 21 '18 at 05:51

2 Answers2

6

Firebase functions only support the specifically named __session cookie to be passed through. If you were handling this manually (setting the Set-Cookie header yourself), it would look something like this:

response.set('Set-Cookie', `__session=${VALUE};`)

You can set VALUE to whatever you like, and it will be set and pass through your functions. Anything else would be stripped out and not available to your function. You can still serialize whatever you want into there as a string, but since you're just checking a time diff, it should be easy enough to check.

technoplato
  • 3,293
  • 21
  • 33
mootrichard
  • 3,581
  • 13
  • 25
1

I'm not sure if Firebase has a function for deleting from the database by age. Here's one way to do it though:

  1. Make a doc with an array of objects that have the data and the time of the last update.

  2. Make a small script with a cleanUp() function and something like this:

const fiveMinutes = 3000000 setInterval(cleanUp, fiveMinutes)

In case you need to deploy, you can run the script with npm forever

Caveman
  • 2,527
  • 1
  • 17
  • 18