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?
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.
I'm not sure if Firebase has a function for deleting from the database by age. Here's one way to do it though:
Make a doc with an array of objects that have the data and the time of the last update.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With