简体   繁体   中英

How to properly use express-session on firebase cloud functions

Hello I am trying to use express sessions in my backend deployed to firebase cloud functions and I trying to use express-sessions to save data related to the current session. but this does not work, I save something using req.session.value = 5 and I try to get that value later and it is undefined.

this is my current express session config.

const session = require('express-session')
const config = require('./config')
const { v4: uuidv4 } = require('uuid');



admin.initializeApp();
const api = require("./api/index");
const app = express();
//app.use(cookieParser());
//app.set('trust proxy',1)
const sess = {
  secret: config.secretKey,
  genid: function(req){
    return uuidv4();
  },
  resave: true,
  saveUninitialized: true,
  cookie: {
    sameSite: false
  }
}
if(app.get('env')==='production'){
  app.set('trust proxy',1)
  sess.cookie.secure = true;

}

app.use(session(sess));

app.use(cors());
app.use(express.json());

and I have a middleware that sets the value based in some conditions

// mymdileware
module.exports = (req, res,next)=>{
if(/* conditions */ ){
req.session.value = 5
next()
}

// other code here with a res.send() 
})

and an endpoint to get the value of the session, and gets executed after the middleware

 app.get("/someEndpontToReadSession", mymdileware,(req, res)=>{
if(req.session.value===5){
   // do something and return something to the user
}
// other code here with a res.json()
})

the problem is when I read req.session.value it is undefined, even though I set it in the middleware.

this works locally but It does not work when I deployed to firebase functions.

Edit: I thought the problem was related to that I am not providing a store to the session config, so I added it. but I am still facing this problem: /

const session = require('express-session')
const config = require('./config')
const { v4: uuidv4 } = require('uuid');
const {Firestore} = require('@google-cloud/firestore');
const {FirestoreStore} = require('@google-cloud/connect-firestore');


admin.initializeApp();
const api = require("./api/index");
const app = express();
//app.use(cookieParser());
//app.set('trust proxy',1)
const sess = {
  secret: config.secretKey,
  genid: function(req){
    return uuidv4();
  },
  resave: false,
  saveUninitialized: true,
  cookie: {
    sameSite: false
  }
}
if(app.get('env')==='production'){

  console.log('production : )');
  app.set('trust proxy',1)
  sess.store = new FirestoreStore({
    dataset: new Firestore(),
    kind: 'express-sessions'
  })
  sess.cookie.secure = true;

}

app.use(session(sess));

Firebase Cloud functions dont allow severals cookies Firebase Doc for their CDN cache, they allow just the cookie named __session so:

app.use(session({ name: "__session",
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {maxAge: 24 * 60 * 60 * 1000}, // 24 hours
store: sessionStorage}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM