Firebase功能:如何存储简单的Cookie来记住经过身份验证的用户 [英] Firebase Functions : How to store simple cookies to remember an authenticated user

查看:87
本文介绍了Firebase功能:如何存储简单的Cookie来记住经过身份验证的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想提醒用户返回网站并仅在5分钟后统计观看次数.我做到了,可以在使用Firebase Serve时使用,但是cookie在部署后不会存储.

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'] }));

尝试检查会话是否存在并且会话时间不超过5分钟.

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

然后我发现问题在于我们必须使用__session.我不太了解.我可以从上面的代码示例中获取有关上下文的示例吗?

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函数仅支持

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

您可以将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.

这篇关于Firebase功能:如何存储简单的Cookie来记住经过身份验证的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆