Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Functions : How to store simple cookies to remember an authenticated user [duplicate]

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?

like image 600
Relm Avatar asked Apr 09 '18 08:04

Relm


2 Answers

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.

like image 123
mootrichard Avatar answered Nov 05 '22 18:11

mootrichard


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

like image 1
Caveman Avatar answered Nov 05 '22 20:11

Caveman