koajs / session

Simple session middleware for koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Firebase Only: New session after each change to session

Sotacan opened this issue · comments

Overview

I'm trying to host the Koa app on Firebase Cloud Functions + Hosting through url rewrites and am having some trouble.

Firebase issue

Firebase functions + firebase hosting with this causes issues. I noticed that data was getting saved to the provided store (firebase) but it would send a new uuid set-cookie header each time I change any data in the session.

One thing to note is firebase strips all cookies besides '__session' (though i did set this as the 'key' parameter which I assume would solve this)

Suggested Solution:

After looking at the library I am not quite sure what the issue is but I was able to create a temporary solution using firestore as the store. here is an example

var KoaFirebaseSession = { set: async (ctx, key, val) => { var ourSessionKey = ctx.cookies.get("__session"); if(!ourSessionKey) { ourSessionKey = uuidv4(); ctx.cookies.set("__session", ourSessionKey); } await firestoreAccess.set(ourSessionKey, { [key]: val, "_expire": Date.now() + COOKIE_CONFIG.maxAge, "_maxAge": COOKIE_CONFIG.maxAge }); }, get: async (ctx, key=null) => { var ourSessionKey = ctx.cookies.get("__session"); if(ourSessionKey) { var sessionData = await firestoreAccess.get(ourSessionKey, COOKIE_CONFIG.maxAge); if(key && sessionData && sessionData[key]) { return sessionData[key]; } else if(sessionData) { return sessionData; } } return {}; } }