将护照和 OAuth 与 connect-redis 结合使用 [英] Using passport and OAuth with connect-redis

查看:51
本文介绍了将护照和 OAuth 与 connect-redis 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用 Redis 进行 Express 会话的应用程序中使用passport-twitter 和passport-facebook 进行身份验证.如果我删除用于在 express 中存储会话的 connect-redis,一切正常,但是对于 Redis 会话,我收到以下错误:

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

我的代码如下:

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

可以一起使用这些吗?

在进一步调试时,似乎将 connect-redis 用于快速会话存储并没有将 req.session 设置为任何内容.这就是导致问题的原因.不幸的是,我似乎根本无法让它发挥作用.其他有同样问题的人已经通过将 cookieParser 行放在会话行上方来修复它,但这一直是我的地方,所以问题是其他问题.我不知所措.

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.

推荐答案

好的,经过两天的搜索和遍历所有涉及的 node_modules 的源代码(express、connect、connect-redis),我找到了我的问题一个简单的愚蠢的错字.

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];

这里的 authToken 只提供密码(这是 redis 用来连接的全部).但是要手动选择数据库,您需要删除 [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!

这篇关于将护照和 OAuth 与 connect-redis 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆