什么是适用于Node.js的最佳Facebook连接库?

21

我看过多种用于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存储在数据库中?

我感觉答案基本上是否定的,我需要在上述系统之一的基础上构建更简单的东西,但我想先确认一下。

编辑:请确保使用node.js的稳定版本。

3个回答

17

你是否没有找到 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);

Facebook设置


1
它对我来说非常好用!你必须设置const fbCallbackAddress =“http://localhost:4000/auth/facebook”; 当然要更改localhost:4000 /部分。 - Alfred
阿尔弗雷德,我可以直接和你聊天吗? - Travis
1
原来是这样,任何人发现这里的情况:在节点v 0.2.6中运行良好,但如果您升级到0.3.7分支,则会进入无限循环。请查看此处的对话:https://github.com/ciaranj/connect-auth/issues#issue/31 - Travis
哦,我的情况是,req.isAuthenticated()和req.authenticate()都不能用。我看到了这个错误消息:“500 TypeError: Object #<IncomingMessage> has no method 'authenticate'”。我正在使用express 2.3.10版本。我该怎么做才能使用这些函数? - Whiteship
我上次检查是在很久以前(使用node v0.2.6 ;))。现在我建议使用everyauth。我已经上传了gist,网址为https://gist.github.com/1039402。依赖项已经在node_modules文件夹中,但您需要配置FacebookId、FacebookSecret和FacebookSiteURL。 - Alfred
显示剩余3条评论

8
我认为passport-facebook相当简单实用。
我也喜欢核心护照模块有80多种身份验证策略。
(如 twitter、 Google、 foursquare、 GitHub、 digg、 dropbox 等)。

从作者的 github 读我中可以看到:

// 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进行身份验证方面表现良好。 - Jorre

1

我使用了Brian Noguchi的everyauth。它适用于node.js v.0.4.x。你可以在这里找到它。

它具有原生支持mongodb的功能,使用了mongoose-auth插件,同样是由Brian编写。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接