benweier / connect-session-firestore Goto Github PK
View Code? Open in Web Editor NEWA Connect/Express session store powered by the Firebase Cloud Firestore.
License: MIT License
A Connect/Express session store powered by the Firebase Cloud Firestore.
License: MIT License
Hi there! I'm trying to get this up and running, but I must have made a mistake somewhere.
With my current setup, I am getting this error:
Error: Argument "data" is not a valid Document. Couldn't serialize object of type "Session". Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the 'new' operator).
The error occurs when this method is called; session
is a Session instance.
Logging it out, I received:
Session {
cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true } }
I followed the express instructions from the README. Here are some relevant versions and code:
"connect-session-firestore": "1.0.0",
"express": "4.16.3",
"express-session": "1.15.6",
"firebase-admin": "5.12.0",
const session = require('express-session');
const FirestoreStore = require('connect-session-firestore')(session);
// This is the return value from `firebase.initializeApp()`
const firebaseAdmin = require('../utils/firebase-admin');
const router = express.Router();
router.use(session({
store: new FirestoreStore({
database: firebaseAdmin.firestore()
}),
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}));
I'm new to Firebase, but it's looking like the express-session is supposed to be passing the session instance.
Is there something I need to configure to enable Firebase to serialize session objects?
Thanks for reading!
Update: The problem seems to be two levels deep. Not only is session
an invalid object for Firestore, but the session.cookie
is, too.
As a super hacky workaround, I am doing this:
let sessionObj = {
cookie: {}
};
for (let key in session) {
const sessionValue = session[key];
if (key === 'cookie') {
const naiveCookie = sessionValue.toJSON();
for (let cookieKey in naiveCookie) {
if (naiveCookie[cookieKey]) {
sessionObj.cookie[cookieKey] = naiveCookie[cookieKey];
}
}
} else if (isPrimitive(sessionValue)) {
sessionObj[key] = sessionValue;
} else if (sessionValue && sessionValue.constructor === Object) {
sessionObj[key] = sessionValue;
}
}
const data = {
expires: expires,
session: sessionObj,
type: 'connect-session'
};
It is far from perfect, and embarrassingly hacky, but it has (for the time being) unblocked me. I'm very curious to learn more about how folks are getting the session object into Firebase without resorting to such crazy hacks!
I've implemented your connect-session-firbase and it works great but whenever I try and use this one the browser just hangs when I try to connect to the application?
Also small mistake in the docs for both repos - you're missing a comma after the secret value of 'keyboard cat'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.