2010-12-19 6 views
21

Ich habe mehrere Tools zum Arbeiten mit node.js und facebook connect gesehen. Viele von ihnen scheinen jedoch unvollständig, zu komplex (nicht abstrakt) oder nicht mehr aktualisiert/gepflegt zu sein.Was ist die beste facebook connect-Bibliothek für node.js?

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

Hier einer der Autoren diskutiert auch, warum er noch einmal seine eigene gerollt:

Ich habe diese drei Projekte gefunden aufgrund von Mängeln in anderen Implementierungen :

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

Hat jemand eine wirklich reale Erfahrung Authentifizierung von Benutzern und ihre Facebook-IDs in ihrer Datenbank zu speichern mit node.js und Facebook verbinden?

Ich habe das Gefühl, dass die Antwort ziemlich nein ist und ich muss auf einem der oben genannten Systeme bauen, um die Dinge viel einfacher zu machen, aber ich wollte zuerst überprüfen.

Edit: Beachten Sie unbedingt die stabile Version von node.js

Antwort

17

Verwenden Sie ciaranj die nicht 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 finden (4000);

facebook settings

+0

Es scheint eine Menge von der Art und Weise zu arbeiten, aber nach unten hängt nur auf Facebooks Umleitung auf/Auth/facebook ... und tut nichts – Travis

+1

es funktioniert wie ein Charme für mich! Sie müssen const fbCallbackAddress = "http: // localhost: 4000/auth/facebook"; offcourse ändern localhost: 4000/Teil – Alfred

+0

geändert Snippet ein wenig. Sie müssen natürlich Auth-Middleware-Variablen angeben. – Alfred

1

Ich habe Brian Noguchis everyauth verwendet. Es funktioniert mit node.js v.0.4.x. Sie können das here finden.

Es hat native Unterstützung für Mongodb mit mongoose-auth Plugin, wieder von brian geschrieben.

8

Ich finde passport-facebook ziemlich einfach und nützlich.
Ich mag auch, dass das Kernpassmodul 80+ Auth-Strategien hat.
(z. B. Twitter, Google, Foursquare, Github, Digg, Dropbox).

Aus Github README Schöpfer:

// 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('/'); 
}); 
+0

Ich habe diese Bibliothek auch benutzt. Funktioniert gut für die Authentifizierung über Facebook. – Jorre

Verwandte Themen