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

查看:32
本文介绍了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 函数仅支持 要通过的具体命名的 __session cookie.如果您手动处理此问题(自己设置 Set-Cookie 标头),它将如下所示:

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天全站免登陆