const functions = require('firebase-functions');
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const bodyParser = require('body-parser');
const FirebaseStore = require('connect-session-firebase')(session);
const {database} = require('./connection');
const app = express();
const userRoute = require('./routes/user/route');
const cropRoute = require('./routes/crop/route');
const landRoute = require('./routes/land/route');
const webRoute = require('./routes/web/route');
app.set('view engine', 'ejs');
app.use(session({
store: new FirebaseStore({
database: database
}),
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
//Allow CORS Automatically
app.use(cors({origin: true}));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/user', userRoute);
app.use('/crop', cropRoute);
app.use('/land', landRoute);
app.use('/', webRoute);
app.get('/session', (req, res, next) => {
req.session.username = "akshay";
req.session.password = "sdlkls";
res.send(req.session.username);
});
app.get('/session1', (req, res) => {
console.log(req.session);
if (req.session.username) {
res.send('true');
}else {
res.send('false');
}
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Page Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
// render the error page
console.log(err);
res.status(err.status || 500);
res.render('404', {status: err.status, message: err.message});
});
exports.api = functions.https.onRequest(app);
When I run this, it creates a new session in the DB.
And every time I refresh the page, there is a new session.
I want of course, that only one session would be created,
and that on refresh, this session would only be updated, or to get the data from there. not to create a new one every time.
I've been working on this for hours...
Same issue with saving sessions in the MongoDB store
@akshaykalola28 - which route you are using that leads to this issue? session, session1, or both?
session1
@akshaykalola28 I am trying to reproduce this issue. Can you please tell me const {database} = require('./connection'); whether it is written by you ?. If yes how can I reproduce the same ?
Yes. I have used Firebase Cloud Firestore. So, database is an instance of that.
@HarshithaKP See the code form the connection.js file as below:
const admin = require('firebase-admin');
admin.initializeApp();
const database = admin.database();
module.exports.database = database;
@akshaykalola28 Sorry for the delayed response.
I was able to recreate your problem with memoryStore and resolved it with small changes in code. This not related with stores.
In session1 route after checking for the username, if doesn't exist you need to create one as it is not created by default. So when the user visits for second time, it is fetched from database.
Please find the working logic below.
app.get('/session1', (req, res) => {
console.log(req.session);
if (req.session.username) {
res.send('true');
}else {
req.session.username = 'foo'
res.send('false');
}
});
Let me know if this works for you.
Most helpful comment
Yes. I have used Firebase Cloud Firestore. So,
databaseis an instance of that.@HarshithaKP See the code form the connection.js file as below:
const admin = require('firebase-admin'); admin.initializeApp(); const database = admin.database(); module.exports.database = database;