f1nn
f1nn

Reputation: 7047

Nodejs + Passport.js + Redis: how to store sessions in Redis

I've read this topic Node.js + express.js + passport.js : stay authenticated between server restart and I need exactly the same thing, but for Redis. I used such code:

var RedisStore = require('connect-redis')(express);
app.use(express.session({
    secret: "my secret",
    store: new RedisStore,
        cookie: { secure: true, maxAge:86400000 }
}));

And it doesn't work. To connect Redis I use connect-redis module. What I'm doing wrong? Thanks!

UPD: I don't get any errors. To ensure auth processes succesfully, I added log-line, and it executes.

function(email, password, done) {
    // asynchronous verification, for effect...
    process.nextTick(function() {
        findByEmail(email, function(err, user) {
            if (!user) {
                return done(null, false, {
                    message: 'Unknown user ' + email
                });
            }
            if (user.password != password) {
                return done(null, false, {
                    message: 'Invalid password'
                });
            }
            //just logging that eveything seems fine
            console.log("STATUS: User " + email + " authentificated succesfully");
            return done(null, user);
        })
    });
}));

Log with express.logger() enabled was:

127.0.0.1 - - [Fri, 19 Oct 2012 05:49:09 GMT] "GET /ico/favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
STATUS: User admin authentificated succesfully

I do suppose that there is nothing wrong with auth/users/credentials/serializing/deserializing itself. The problem is just passport cannot set cookie to Redis and the read it.

Upvotes: 16

Views: 20409

Answers (3)

pixelus
pixelus

Reputation: 11

try this out, instead of passing express to const RedisStore pass session.

const redis = require('redis');
const session = require('express-session');
const redisStore = require('connect-redis')(session);
const cookieParser = require('cookie-parser');
const app = require('../app');


app.app.use(cookieParser("secret"));


const rediscli = redis.createClient();


app.app.use(session({
    secret: 'secret',
    store: new redisStore({
        host: '127.0.0.1',
        port: 6379,
        client: rediscli,
        ttl: 260
    }),
    saveUninitialized: false,
    resave: false
}));

Upvotes: 1

Ben
Ben

Reputation: 7597

What happens when you set the store explicitly? i.e. something along these lines in your app:

var redis = require('redis');
// This is host and port-dependent, obviously
var redisClient= redis.createClient(6379, 'localhost');

app.use(express.session({
    secret: 'your secret',
    /* set up your cookie how you want */
    cookie: { maxAge: ... },
    store: new (require('express-sessions'))({
        storage: 'redis',
        instance: redisClient
    })
}));

Upvotes: 0

f1nn
f1nn

Reputation: 7047

I should use

cookie: { secure: false, maxAge:86400000 }

Upvotes: 6

Related Questions