node.js - How to find a session from MongoDB collection using Express and MongoStore -
i implementing session store using node/express/mongo. problem cannot retrieve sessions sessions
collection, unable determine whether user has logged in.
i using mongoskin, mongostore, , mongo-connect, although don't mind using mongoose , other tools.
here debug output:
// check whether current user has session. // if so, adds "currentuser" request. // else, redirect login page. function loaduser(req, res, next) { console.log("loaduser: checking loaduser..."); var db = req.db; console.log("current req.session.token:"); console.log(req.session.token); console.log("current req.session:"); console.log(req.session); if (req.session.token) { // session id in database 'sessions' collection. db.collection('sessions').findone({token : req.session.token}, function(err, user) { console.log("found user looking token:"); console.log(user); if (user) { req.currentuser = user; next(); } else { console.log("token not in 'sessions', redirecting..."); res.redirect('/login/webapp_login.html'); } }); } else { console.log("no token, redirecting..."); res.redirect('/login/webapp_login.html'); } }
and here console output:
loaduser: checking loaduser... current req.session._id: 53acb8e1b7b297dc29681def current req.session: { cookie: { path: '/', _expires: null, originalmaxage: null, httponly: true }, islogged: 21, username: 'sessiontest1', token: '57a6dfe0bea9150bd4e2d1f76974e88b', } found user looking token: null token not in 'sessions', redirecting...
along with:
db.collection('sessions').find(req.session._id, function(user) {
i tried:
db.collection('sessions').findone({session:{token:req.session.token}}, function(err, user) {
but still null
, function redirects. missing something? suggestions helpful. using mongoskin, although open solutions in mongoose well.
additionally, know database configured correctly because checked command line:
> db.sessions.find().pretty() { "_id" : "vnftbgnfvq3s4lhiib_omoxwdu01kfuh", "session" : "{\"cookie\":{\"originalmaxage\":null,\"expires\":null,\"httponly\":t rue,\"path\":\"/\"},\"islogged\":6}", "expires" : isodate("2014-07-09t03:44:54.863z") } { "_id" : "-amkc_kizooan_eqj6rjpvtgwoarglaj", "session" : "{\"cookie\":{\"originalmaxage\":null,\"expires\":null,\"httponly\":t rue,\"path\":\"/\"},\"islogged\":20,\"username\":\"sessiontest1\",\"token\":\"57a6dfe0bea 9150bd4e2d1f76974e88b\"}", "expires" : isodate("2014-07-11t06:35:14.835z") }
what going wrong, , why can't retrieve session sessions
collection?
update running
db.collection('sessions').find({"session" : /./}, function(err, user) {
gives me output, believe problem matching record field within session field's string. session
field, shown in database output above, problematic because 1 long string , not nested json.
also, sessions
records inserted automatically after adding app.js
:
app.use(expresssession({ secret: 's3cretc0de', store: new mongostore({ url: mongourl }, function () { console.log("db session connection open"); }) }));
and added token
, username
fields within router, shown below:
router.get('/verify', function(req, res) { req.session.username = username; req.session.token = req.query.token; }
am missing something? appreciated.
when use mongostore storing session data don't need query mongodb directly. module you.
you can use req.session
session data.
so in routes can like:
app.get('/', function(req, res){ // check if username set in session if (!req.session.username) { // redirect login page res.redirect('/login'); } else { // } }); app.post('/login', function(req, res) { // check check username & password here // if (username == '...' /&& password ...) // set username in session session req.session.username = username; });
Comments
Post a Comment