【问题标题】:Everyauth and accessing the (twitter) oauth data from within findUserByIdEveryauth 并从 findUserById 中访问 (twitter) oauth 数据
【发布时间】:2012-04-02 12:18:45
【问题描述】:

我正在尝试在express 中使用everyauth 来验证用户对允许向图像添加文本评论的网站的身份。我目前正在使用 twitter oauth api 来验证我的用户。

现在我正在尝试建立一个自定义用户数据库,该数据库允许我将多种登录方式连接到 CouchDB 中的单个条目。当我想在他最初通过 twitter 连接时将新用户添加到数据库时,我遇到了麻烦。我findUserById的代码如下:

everyauth.everymodule.findUserById (userId, callback) ->  
    users.findById userId, (err,res) ->
        if res.id    
        # id returned, so there is an entry in the DB
            callback null, res
        else         
        # no entry in the DB, add a new one
            users.saveById JSON.stringify(userId), {"name":"test"}, (saveErr, saveRes) ->
                callback null, saveRes

everyauth.twitter
    .consumerKey(config.twitterConsumerKey)
    .consumerSecret(config.twitterConsumerSecret)
    .findOrCreateUser((session, token, secret, user) ->
         promise = @.Promise().fulfill user
    ).redirectPath '/'

findByIdsaveById 是查询数据库的cradle 对象的方法,userId 似乎是由everyauth 提供的。

我的问题在于{"name":"test"} 部分。这是我要将req.session.auth.twitter 对象中的数据添加到数据库的位置。 如何从findUserByIdfunction 中访问这些值?

这可能吗?有不同的方法吗?我正在查看 findOrCreateUser 函数,但我不知道如何在不使我的应用崩溃的情况下更改它 - 我还没有掌握 Promise 的概念。

【问题讨论】:

    标签: node.js express everyauth promise


    【解决方案1】:

    我无法就如何使用everyauth 解决您的问题提供任何指导。但是,您是否考虑过使用Passport 进行身份验证?

    我是 Passport 的开发人员,如有任何问题,请随时问我。我在尝试了everyauth 之后写了它,并且对提供的流控制(包括承诺)同样感到沮丧。

    Passport 有一个简单的机制来处理使用 Twitter(或任何其他 OAuth 提供程序)的登录,使用 passport-twitter 模块:

    passport.use(new TwitterStrategy({
        consumerKey: TWITTER_CONSUMER_KEY,
        consumerSecret: TWITTER_CONSUMER_SECRET,
        callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
      },
      function(token, tokenSecret, profile, done) {
        User.findOrCreate({ twitterId: profile.id }, function (err, user) {
          return done(err, user);
        });
      }
    ));
    

    在验证回调(众所周知)中,您可以完全访问 Twitter 颁发的令牌和 tokenSecret,以及完整的 Twitter 个人资料。您可以使用该信息在数据库中查找或创建用户。

    触发登录就像向您的应用添加几个路由一样简单:

    app.get('/auth/twitter', passport.authenticate('twitter'));
    
    app.get('/auth/twitter/callback', 
      passport.authenticate('twitter', { successRedirect: '/',
                                         failureRedirect: '/login' }));
    

    【讨论】:

    • 感谢您的详细解释。我确实对 Promise 部分感到困惑,从我读到的内容来看,这种范式被 node 留下了。但是护照看起来很有前途,我将在我的下一个大项目中使用它:)
    【解决方案2】:

    在进一步摆弄之后,我找到了解决我提出的问题的方法。要访问通过 OAuth 请求传输的关于everyauth 的 Promise 结构的完整用户数据,everyauth.twitter 调用应如下所示:

    everyauth.twitter
     .consumerKey(config.twitterConsumerKey)
     .consumerSecret(config.twitterConsumerSecret)
     .findOrCreateUser((session, token, secret, user) ->
       users.findByTwitterId user.id, (err,res) ->
         if res.id
           user=res.value
         else
           newUser = {id:user.id,name:user.name,twitter:user}
           user = newUser
           users.saveById JSON.stringify(user.id), user, (saveErr, saveRes) -> 
       promise = @.Promise().fulfill user
     ).redirectPath '/'
    

    findByTwitterId 调用是一个函数,用于查询 CouchDB 以查找查找包含 twitter.id 字段的文档的视图。

    req.user 对象由以下内容填充:

    everyauth.everymodule.findUserById (userId, callback) ->  
     users.findByTwitterId userId, (err,res) ->
        if res.id
            callback null, res.value
        else
            callback null, null
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 2016-06-15
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2014-03-09
      相关资源
      最近更新 更多