Hitu Bansal
Hitu Bansal

Reputation: 3137

Can we store session in Database?

Can we store session in database Mysql not in memory using passport module.

I am using nodejs, express and mysql

Thanks

Upvotes: 0

Views: 2289

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51490

Passport module doesn't provide sessions to your application, it uses connect or express session.

connect/express sessions may (and should!) be persistent and there are plenty of session stores available on npm.

With a quick search I found two implementations of connect/express mysql session store:

Just take any one of them and use as your session store with express-session (or conncect.session):

var express = require('express');
var session = require('express-session');
var SessionStore = require('express-mysql-session')

var app = express();
var sessionStore = new SessionStore(/*options*/);

app.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: sessionStore,
    resave: true,
    saveUninitialized: true
}))

Upvotes: 4

Related Questions