Firebase云功能将不存储名称为“ __session”的Cookie。 [英] firebase cloud function won't store cookie named other than "__session"

查看:71
本文介绍了Firebase云功能将不存储名称为“ __session”的Cookie。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了授权-https-endpoint 并且仅添加console.log来打印req.cookies,问题是cookie始终为空 {} 我使用客户端JS调用设置了cookie,但确实保存了

i followed the sample of authorized-https-endpoint and only added console.log to print the req.cookies, the problem is the cookies are always empty {} I set the cookies using client JS calls and they do save but from some reason, I can't get them on the server side.

这是index.js的完整代码,它与示例完全相同:

here is the full code of index.js, it's exactly the same as the sample:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cookieParser = require('cookie-parser')();
const cors = require('cors')({origin: true});
const app = express();

const validateFirebaseIdToken = (req, res, next) => {
  console.log(req.cookies); //// <----- issue this is empty {} why?? 
  next();
};

app.use(cors);
app.use(cookieParser);
app.use(validateFirebaseIdToken);
app.get('/hello', (req, res) => {
  res.send(`Hello!!`);
});

exports.app = functions.https.onRequest(app);

商店cookie:

卷曲http:// FUNCTION_URL / hello --cookie __session = bar // req.cookies =
{__session:bar}

不存储:

curl http:// FUNCTION_URL / hello --cookie foo = bar // req.cookies =
{}

推荐答案

如果您使用的是Firebase Hosting + Cloud函数, __ session 是设计上可以存储的唯一cookie。为了使我们能够有效地在CDN上缓存内容,这是必要的-我们从请求中剥离除 __ session 之外的所有cookie。应该将其记录下来,但似乎没有(糟糕!)。我们将更新文档以反映此限制。

If you are using Firebase Hosting + Cloud Functions, __session is the only cookie you can store, by design. This is necessary for us to be able to efficiently cache content on the CDN -- we strip all cookies from the request other than __session. This should be documented but doesn't appear to be (oops!). We'll update documentation to reflect this limitation.

此外,您需要将Cache-Control Header设置为private

Also, you need to set Cache-Control Header as private

res.setHeader('Cache-Control', 'private');

这篇关于Firebase云功能将不存储名称为“ __session”的Cookie。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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