【问题标题】:What is the best facebook connect library for node.js?node.js 的最佳 Facebook 连接库是什么?
【发布时间】:2011-05-27 22:56:55
【问题描述】:

我见过多种使用 node.js 和 facebook connect 的工具。然而,其中许多似乎不完整、过于复杂(非抽象)或不再更新/维护。

我找到了这三个项目:

https://github.com/DracoBlue/node-facebook-client

https://github.com/dominiek/node-facebook

https://github.com/egorFiNE/facebook-connect

https://github.com/ciaranj/node-oauth

这里的一位作者甚至讨论了为什么他再次推出自己的产品,因为其他实现存在缺陷:

http://groups.google.com/group/nodejs/browse_thread/thread/bb46cb08e51fdda6

有没有人有任何实际使用 node.js 和 facebook connect 验证用户身份并将他们的 facebook id 存储在他们的数据库中的实际经验?

我觉得答案几乎是否定的,我必须在上述系统的基础上构建以使事情变得更简单,但我想先检查一下。

编辑:请注意确保您使用的是 STABLE 版本的 node.js

【问题讨论】:

    标签: javascript facebook node.js


    【解决方案1】:

    你没有找到ciaranj 的connect-auth

    const fbId = ""; #x
    const fbSecret = ""; #y
    const fbCallbackAddress= "http://localhost:4000/auth/facebook";
    //var RedisStore = require('connect-redis');
    var express= require('express');
    var auth= require('connect-auth')
    var app = express.createServer();
    app.configure(function(){
      app.use(express.cookieDecoder());
      app.use(express.logger());
      //app.use(connect.session({ store: new RedisStore({ maxAge: 10080000 }) }));
      app.use(express.session());
      app.use(auth( [
        auth.Facebook({appId : fbId, appSecret: fbSecret, scope: "email", callback: fbCallbackAddress})
      ]) );
    });
    
    
    app.get('/logout', function(req, res, params) {
        req.logout();
        res.writeHead(303, { 'Location': "/" });
        res.end('');
    });
    
    app.get('/', function(req, res, params) {
        if( !req.isAuthenticated() ) {
            res.send('<html>                                              \n\
              <head>                                             \n\
                <title>connect Auth -- Not Authenticated</title> \n\
                <script src="http://static.ak.fbcdn.net/connect/en_US/core.js"></script> \n\
              </head><body>                                             \n\
                <div id="wrapper">                               \n\
                  <h1>Not authenticated</h1>                     \n\
                  <div class="fb_button" id="fb-login" style="float:left; background-position: left -188px">          \n\
                    <a href="/auth/facebook" class="fb_button_medium">        \n\
                      <span id="fb_login_text" class="fb_button_text"> \n\
                        Connect with Facebook                    \n\
                      </span>                                    \n\
                    </a>                                         \n\
                  </div></body></html>');
        } else {
             res.send( JSON.stringify( req.getAuthDetails()) );
        }
    });
    
    // Method to handle a sign-in with a specified method type, and a url to go back to ...
    app.get('/auth/facebook', function(req,res) {
      req.authenticate(['facebook'], function(error, authenticated) { 
         if(authenticated ) {
            res.send("<html><h1>Hello Facebook user:" + JSON.stringify( req.getAuthDetails() ) + ".</h1></html>")
          }
          else {
            res.send("<html><h1>Facebook authentication failed :( </h1></html>")
          }
       });
    });
    

    app.listen(4000);

    【讨论】:

    • 它似乎有很多工作,但只是挂在 facebooks 重定向到 /auth/facebook ... 并且什么都不做
    • 它对我来说就像一个魅力!你必须设置 const fbCallbackAddress= "localhost:4000/auth/facebook";课程更改 localhost:4000/ 部分
    • 稍微改变了 sn-p。您必须指定身份验证中间件变量。
    • 结果发现任何人都发现了这里发生的事情:它在节点 v 0.2.6 中运行良好,但如果您向上移动到 0.3.7 分支,它会进入无限循环。在这里查看 convo:github.com/ciaranj/connect-auth/issues#issue/31
    【解决方案2】:

    我发现passport-facebook 相当简单实用。
    我也喜欢核心护照模块有 80 多种身份验证策略。
    (例如 twitter、google、foursquare、github、digg、dropbox)。

    来自创建者的 github README:

    // Set up the strategy
    passport.use(new FacebookStrategy({
            clientID: FACEBOOK_APP_ID,
            clientSecret: FACEBOOK_APP_SECRET,
            callbackURL: "http://localhost:3000/auth/facebook/callback"
        },
        function(accessToken, refreshToken, profile, done) {
            User.findOrCreate({ facebookId: profile.id }, function (err, user) {
                return done(err, user);
            });
        }
    ));
    
    // Use the authentication
    app.get('/auth/facebook',
        passport.authenticate('facebook'),
        function(req, res){
            // The request will be redirected to Facebook for authentication, so
            // this function will not be called.
    });
    
    app.get('/auth/facebook/callback',
        passport.authenticate('facebook', { failureRedirect: '/login' }),
        function(req, res) {
            // Successful authentication, redirect home.
            res.redirect('/');
    });
    

    【讨论】:

    • 我也用过这个库。非常适合通过 Facebook 进行身份验证。
    【解决方案3】:

    我使用过 Brian Noguchi 的everyauth。它与 node.js v.0.4.x 一起工作。你可以找到here。

    它使用 mongoose-auth 插件原生支持 mongodb,同样由 brian 编写。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2012-08-08
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多