15

我已经阅读了这个主题Node.js + express.js + passport.js :在服务器重新启动之间保持身份验证,我需要完全相同的东西,但对于 Redis。我使用了这样的代码:

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

它不起作用。要连接 Redis,我使用 connect-redis 模块。我做错了什么?谢谢!

UPD:我没有收到任何错误。为了确保身份验证过程成功,我添加了日志行,它会执行。

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);
        })
    });
}));

启用 express.logger() 的日志是:

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

确实认为 auth/users/credentials/serializing/deserializing 本身没有任何问题。问题只是护照无法将cookie设置为Redis并读取它。

4

3 回答 3

6

我应该使用

cookie: { secure: false, maxAge:86400000 }
于 2012-10-19T16:43:05.807 回答
1

试试这个,而不是通过快递来const RedisStore通过会话。

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
}));
于 2019-11-06T04:45:08.133 回答
0

当您明确设置商店时会发生什么?即在您的应用程序中的这些内容:

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
    })
}));
于 2014-10-21T12:45:00.243 回答