javascript node.js 最好的 Facebook 连接库是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4484825/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best facebook connect library for node.js?
提问by Travis
I've seen multiple tools for working with node.js and facebook connect. However many of them seem incomplete, overly-complex (non abstract) or no longer updated/maintained.
我见过多种使用 node.js 和 facebook connect 的工具。然而,其中许多看起来不完整、过于复杂(非抽象)或不再更新/维护。
I've found these three projects:
我找到了这三个项目:
https://github.com/DracoBlue/node-facebook-client
https://github.com/DracoBlue/node-facebook-client
https://github.com/dominiek/node-facebook
https://github.com/dominiek/node-facebook
https://github.com/egorFiNE/facebook-connect
https://github.com/egorFiNE/facebook-connect
https://github.com/ciaranj/node-oauth
https://github.com/ciaranj/node-oauth
Here one of the authors even discusses why he once again rolled his own, due to shortcomings in other implementations:
这里一位作者甚至讨论了为什么他再次推出自己的,由于其他实现的缺点:
http://groups.google.com/group/nodejs/browse_thread/thread/bb46cb08e51fdda6
http://groups.google.com/group/nodejs/browse_thread/thread/bb46cb08e51fdda6
Does anyone have any real experience actually authenticating users and storing their facebook id's in their database using node.js and facebook connect?
有没有人有实际使用 node.js 和 facebook connect 对用户进行身份验证并将他们的 facebook id 存储在他们的数据库中的真实经验?
I have a feeling that the answer is pretty much no and I'll have to build on top of one of the above systems to make things much simpler, but I wanted to check first.
我有一种感觉,答案几乎是否定的,我必须建立在上述系统之一之上才能使事情变得更简单,但我想先检查一下。
Edit: Note make sure you use the STABLE version of node.js
编辑:请注意确保您使用 node.js 的稳定版本
采纳答案by Alfred
Did you not find ciaranj's connect-auth
没找到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);
app.listen(4000);


回答by treejanitor
I find passport-facebookfairly simple and useful.
I also like that the core passport module has 80+ auth strategies.
(e.g. twitter, google, foursquare, github, digg, dropbox).
我发现passport-facebook相当简单和有用。
我也喜欢核心护照模块有 80 多种身份验证策略。
(例如 twitter、google、foursquare、github、digg、dropbox)。
From the creator's github README:
来自创建者的 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('/');
});
回答by alexandru.topliceanu
I have used Brian Noguchi's everyauth. It works w/ node.js v.0.4.x. You can find that here.
我使用了 Brian Noguchi 的 Everyauth。它适用于 node.js v.0.4.x。你可以在这里找到。
It has native support for mongodb using mongoose-authplugin, again written by brian.
它使用mongoose-auth插件对 mongodb 提供本机支持,同样由 brian 编写。

