Using passport and OAuth with connect-redis

冷暖自知 提交于 2019-12-24 21:18:30

问题


I am trying to use passport-twitter and passport-facebook for authentication in an app that is using Redis for Express sessions. If I remove the connect-redis for storing sessions in express, everything works fine, but with the Redis sessions, I get the following error:

Error: OAuth authentication requires session support
| at Strategy.OAuthStrategy.authenticate

My code is below:

app.configure(function(){
app.set('port', process.env.PORT || port);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser("supersecret"));
app.use('/html', express.static('html'));
app.use('/assets', express.static('assets'));
});

if (process.env.REDISCLOUD_URL) {
var rtg = require("url").parse(process.env.REDISCLOUD_URL),
    redis = require('redis'),
    connectRedisStore = require('connect-redis')(express),
    authToken = rtg.auth.split(":")[1];

app.configure(function(){
    app.use(
        express.session(
            {
                secret: 'supersecret',
                cookie: { maxAge: 60000 },
                store: new connectRedisStore({
                    host: rtg.hostname,
                    port: rtg.port,
                    db: authToken[0],
                    pass: authToken[1]
                })
            }
        )
    );

    app.use(passport.initialize());
    app.use(passport.session(
        {
            secret: 'supersecret',
            store: new connectRedisStore({
                host: rtg.hostname,
                port: rtg.port,
                db: authToken[0],
                pass: authToken[1]
            })
        }
    ));
});
}

Is it possible to use these together?

Edit: On further debugging, it seems that using connect-redis for the express session store is not setting the req.session to anything. This is what is causing the problem. Unfortunately, I can't seem to make it work at all. Other people with the same issue have fixed it by putting the cookieParser line above the session line, but that's where it's always been in mine, so the issue is something else. I am at a loss.


回答1:


Ok, after two days of searching and poking through the source code of all the node_modules involved (express, connect, connect-redis), I tracked down my problem to a simple stupid typo.

authToken = rtg.auth.split(":")[1];

The authToken here just provides the password (which is all that redis uses to connect). But to manually choose a database, you need to remove the [1] and pass in the first element as the db name, and the second as the pass. Hope it helps someone else out there!



来源:https://stackoverflow.com/questions/16523991/using-passport-and-oauth-with-connect-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!