here is my config:
const session = require('express-session');
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: false,
}))
set in route:
req.session.challenge = makeCredChallenge.challenge;
req.session.user_name = body.user_name;
using in other route:
console.log('challange from session', req.session.challenge)
console.log('username from session', req.session.user_nam
e)
but its undefine in other routes where I'm using it
After searching around about it, I can't find any solution or mistake in my code about this error. I've got my app.js files inside my node JS application with the mongo-connect declaration :
const MongoStore = require('connect-mongo')(session)
And I've got this error :
TypeError: Class constructor MongoStore cannot be invoked without
'new'
at Object. (/Users/souhailmohamed/project/devops/story website/app.js:11:20)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
There is my app.use code beloww :
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
I understand pretty well about the
const MongoStore = require('connect-mongo')(session)
but I didn't understand the error. But it's from a tutorial from youtube by traversy media LinK
connect-mongo v4 introduces new signature (compared to v3).
Here is the official migration guide.
Also, here is a quick diff of what I changed in my specific project (using mongoose and dotenv) to upgrade from connect-mongo v3 to v4:
I had the same problem in my code and solved it using this:
const MongoDbStore = require('connect-mongo');
Remove (session) from the require statement and update the app.use() like this:
// Session Config
app.use(
session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoDbStore.create({
mongoUrl: YourDatabaseURL
})
})
);
I was following the same so I discovered an npm pakage connect-mongodb-session.
Install it and use the same code of session as Brad uses in his video
const MongoDBStore = require('connect-mongodb-session')(session);
//sessions
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUnitialized: false,
store: new MongoDBStore({
mongooseConnection: mongoose.connection
})
//cookie: { secure: true }
}))
const store = new MongoStore({
uri: 'your mongo uri' ,
collection:'mySessions'
})
//express middleware
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: store
// Don't create a session until something is stored
// cookie: { secure: true } - this wont work without https
}))
It solved my relogging issue also. If you close the tab and renter to localhost you get yourself logged in already
Ok so i've found a way to resolve it, i don't about the reason, on which version of express to use but i removed the (session) after mongo-connect and change the app.use to this :
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: 'mongodb+srv://<id+ password>#cluster0.cq7f2.mongodb.net/DBname?retryWrites=true&w=majority' })
}))
Certain rule are changed for the syntax in "connect-mongo" document here
const MongoStore = require("connect-mongo");
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI //(URI FROM.env file)
})
//stroed it in database
}));
In your app.js make the following changes:
let MongoStore = require('connect-mongo');
app.use(
session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: 'your mongo url'
})
});
);
References:
https://www.npmjs.com/package/connect-mongo
It seems that 'connect-mongo' is obsolete and the newer version includes 'connect-mongodb-session'
const MongoDBStore = require('connect-mongodb-session')(session);
Use the above code instead and it should work without any errors.
Note: install the 'connect-mongodb-session' package using npm init connect-mongodb-session
I have same problem. Here is the solution:
var session = require('express-session');
var MongoStore = require('connect-mongo');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
app.use(session({
secret : 'mysecretkey',
resave : true,
saveUninitialized : true,
store :MongoStore.create({ mongoUrl: DBUrl })
}));
app.use(session({
store: MongoStore.create({ mongoUrl: 'mongodb://localhost/test-app' })
}));
// Advanced usage
app.use(session({
store: MongoStore.create({
mongoUrl: 'mongodb://user12345:foobar#localhost/test-app?authSource=admin&w=1',
mongoOptions: advancedOptions // See below for details
})
}));
Here is the solution:
const session = require('express-session');
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
app.use(session({
secret: 'story book',
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongoUrl: mongoose.connection._connectionString,
mongoOptions: {}
})
}))
Just use the another npm package connect-mongodb-session and it should work
https://www.npmjs.com/package/connect-mongodb-session
const MongoDBStore = require('connect-mongodb-session')(session);
What is conventional time to set (MaxAge) for my cookies in my Rest Api and I am using connect-mongo package to save the session on My mongodb, How do i destroy of delete the session from my mongodb once the user logged out.
The setup for my cookie is
app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
cookie: {
maxAge: 60000 * 30
}
}));
And the for the authentication am using passport
I used express-session for my Node.js application with these options
app.use(
session({
secret: "mysecret",
resave: true,
saveUninitialized: true,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
cookie: {
httpOnly: false,
expires: new Date(253402300000000)
}
})
);
and sometime when I update the mobile app due to changes the session got lost and I have to logout and login again.
I have the same problem that these questions but in NodeJS:
different session with url's with-www and without-www
Same URL without www different session
If I write domain.com is a different session that www.domain.com ... Why?
It's for some domain rule?
I share here my cookie in express:
app.use(session({
saveUninitialized: true,
resave: true,
secret: config.sessionSecret,
cookie: {
maxAge: 15778476000,
httpOnly: true,
secure: false
},
key: 'sessionId',
store: new mongoStore({
db: db.connection.db,
collection: config.sessionCollection
})
}));
Thank you!