Controlling Session Start with LocomotiveJS

179 views Asked by At

I know it is possible to control session start with express and connect middleware. Like suggested here: Controlling Session Start with express and connect middleware

Is it possible to control session start with LocomotiveJS?

I don't want to start session for every user - I just want to start it when user successfully logs in.

I'm already storing session in MongoDb (myabe in the future I will use Redis) and creating not required session for every user which enter the page seems to be a waste of resources.

1

There are 1 answers

1
irla On BEST ANSWER

I just found the solution: Maybe LcomotiveJS itself does not support it directly, but it can be easily implemented using Express (and Locomotive is on top of it).

Instead of declaring session as:

this.use(express.cookieParser());
this.use(express.session({
    secret: '123qwemax',
    cookie: {maxAge: 60 * 60 * 1000},
    store: new MongoStore({
      url: 'mongodb://localhost:27017/tests'       
    }),
}));

Declare your session as:

this.get(/^\/login\/.*/, express.cookieParser());
this.get(/^\/login\/.*/, express.session({
    secret: '123qwemax',
    cookie: {maxAge: 60 * 60 * 1000},
    store: new MongoStore({
      url: 'mongodb://localhost:27017/tests'       
    }),
}));

This way session will start any time we will go to /login/.* pages.

I don't know why this.all was not working...