Git Product home page Git Product logo

express-sessions's Introduction

express-sessions

ExpressJS/Mongoose Session Storage

Installation

npm install express-sessions

Usage

var mongoose = require('mongoose');

mongoose.connect();

app.use(express.session({
    secret: 'a4f8071f-c873-4447-8ee2',
    cookie: { maxAge: 2628000000 },
    store: new (require('express-sessions'))({
        storage: 'mongodb',
        instance: mongoose, // optional
        host: 'localhost', // optional
        port: 27017, // optional
        db: 'test', // optional
        collection: 'sessions', // optional
        expire: 86400 // optional
    })
}));

Or

var redis = require('redis');
var client = redis.createClient(6379, 'localhost');

app.use(express.session({
    secret: 'a4f8071f-c873-4447-8ee2',
    cookie: { maxAge: 2628000000 },
    store: new (require('express-sessions'))({
        storage: 'redis',
        instance: client, // optional
        host: 'localhost', // optional
        port: 6379, // optional
        collection: 'sessions', // optional
        expire: 86400 // optional
    })
}));

That's it!

express-sessions's People

Contributors

konteck avatar swissgt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

express-sessions's Issues

MongoStore.destroy not removing the stored session

I unfortunately don't have a simple test case to recreate for this. In my application, req.session.destroy() was not removing the session data from the database (effectively disabling the "sign out" functionality).

I found it have something to do with the callback being passed to remove. Changing the code to pass a valid, but noop callback fixed the issue.

    destroy: function (sid, cb) {
        MongoStore.client.remove({ sid: sid }, cb || function(){});
    },

The associated versions of modules I'm using are:

By the way, thanks for sharing this module!

memory leak / security problem when used with express 3.4.5

Hi,

Thanks for express-sessions - nice lib, and the only one I found that really works ;)

I hit a problem when using express 3.4.5 (may also affect other 3.x.y versions), which seems to be because of a version clash with express-sessions dependency on 2.x.y.

To see the problem:
(1) Create a new express project with 3.x.y, using "express myapp".
(2) Add the following middleware to app.js:

var inc = 0;
app.use(function(req,res,next){
  res.locals["" + inc] = inc++;
  console.log(res.locals);
  next();
});

(3) start the app and hit the app a few times with curl or a browser - you'll see that the same res.locals is re-used and just grows forever. This also means that any private information you might put in res.locals is available to the next request!

{ '0': 0, '1': 1, '2': 2, ... }

Without express-sessions res.locals is a new object on each request and you only see the latest value of inc, e.g.:

{ '3': 3 }

Just changing the dependency version to 3.4.5 fixes the problem for me.

mongo session expiry is out by factor of 1000

Mongo session expiry uses a TTL index. The index is created by this code in index.js:

var schema = new mongoose.Schema({
    sid: { type: String, required: true, unique: true },
    data: { type: {} },
    lastAccess: { 
        type: Date, 
        index: { 
            expires: parseInt(options.expire) * 1000
        } 
    },
    expires: { type: Date, index: true }
});

The issue here is that expires is set to options.expire*1000, as if expecting to receive a value in seconds and convert it to milliseconds. However, if we look at the index that gets created:

{
"v" : 1,
"name" : "lastAccess_1",
"key" : {
    "lastAccess" : 1
},
"ns" : "mayqat.sessions",
"expireAfterSeconds" : 60000,
"background" : true,
"safe" : null
}

Notice that mongodb actually calls the value "expireAfterSeconds".

Easy fix: don't multiply by 1000 :)

I have forked and fixed, and could provide a pull request, though i've changed one other thing also (for issue 3).

Destroy session event

Hello and thanks for your wonderful package that make life a bit easier!
My request is to implement an event handler when you want to delete the session ( before deleting). I understand that apparently destroy session fires based on maxAge but it is not always the case. For example if user delete the SID id in his browser , then a new session key will be created and the previous session will be destroyed without any notification.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.