Skip to content

baseUrlField #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ function session(options) {

// pathname mismatch
var originalPath = parseUrl.original(req).pathname;
if(cookieOptions.baseUrlField){
cookieOptions.path = req.baseUrl;
};
if (originalPath.indexOf(cookieOptions.path || '/') !== 0) return next();

// ensure a secret is available or bail
Expand Down
32 changes: 32 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ describe('session()', function(){
assert.equal(typeof session.MemoryStore, 'function')
})

it('should session only exists in the baseUrl', function(done) {
var ctrl_sess = session({
secret: 'keyboard cat',
cookie: {
httpOnly: true,
baseUrlField: true
}
});
var ctrl_corp = function(req, res, next) {
if (!req.session.corp) {
req.session.corp = req.params.corp;
res.send(req.session.corp);
} else if (req.session.corp !== req.params.corp) {
res.send('oh no, session is across.');
} else {
res.send(req.session.corp);
}
};

var app = express();
app.use('/:corp', ctrl_sess, ctrl_corp);

request(app)
.get('/github')
.expect(200, 'github', function(err, res) {
if (err) return done(err)
request(app)
.get('/google')
.expect(200, 'google', done);
});
})

it('should do nothing if req.session exists', function(done){
function setup (req) {
req.session = {}
Expand Down