Skip to content

Instantly share code, notes, and snippets.

@frostbytten
Created June 12, 2011 11:04
Show Gist options
  • Save frostbytten/1021439 to your computer and use it in GitHub Desktop.
Save frostbytten/1021439 to your computer and use it in GitHub Desktop.
Connect-Auth Facebook Example
// Example taken from https://github.com/ciaranj/connect-auth/issues/45
var fbId= "xxxxxxxxxxxxx";
var fbSecret= "xxxxxxxxxxxxxx";
var fbCallbackAddress= "http://localhost:3000/auth/facebook_callback"
var cookieSecret = "node"; // enter a random hash for security
var express= require('express');
var auth = require('connect-auth')
var app = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret: cookieSecret}));
app.use(auth([
auth.Facebook({
appId : fbId,
appSecret: fbSecret,
callback: fbCallbackAddress,
scope: 'email',
failedUri: '/noauth'
})
]));
app.use(app.router);
});
app.get('/auth/facebook', function(req, res) {
req.authenticate("facebook", function(error, authenticated) {
if (authenticated) {
res.redirect("/great");
}
});
});
app.get('/noauth', function(req, res) {
console.log('Authentication Failed');
res.send('Authentication Failed');
});
app.get('/great', function( req, res) {
res.send('Supercoolstuff');
});
app.listen(3000);
@guiomie
Copy link

guiomie commented Jul 26, 2011

I tried this code, when I try to access "res.redirect("/myurl");" I keep getting the following error from facebook has a response in my browser:

{
"error": {
"type": "OAuthException",
"message": "redirect_uri isn't an absolute URI. Check RFC 3986."
}
}

The uri isnt absolute, the error makes sense, but why would it work in your code?

Thank you.

@frostbytten
Copy link
Author

I will check into this. Maybe OAuth library changed something. Which line is your res.redirect failing on?

@guiomie
Copy link

guiomie commented Jul 26, 2011

I would believe it is on line 35. I simply am not redirected to "/great" but some facebook graph api url that displays the json error. Could the absolute URI specification has been implemented after this gist? Or maybe I have something not configured properly in my facebook app settings, but it does seem good.

@frostbytten
Copy link
Author

frostbytten commented Jul 30, 2011 via email

@EugeneLiang
Copy link

Hello, may I know if there's any updates to this gist?

I am receiving errors where no data is returned.

@frostbytten
Copy link
Author

This was a proof-of-concept for a bug fix which has now been released. Please see the example application from https://github.com/ciaranj/connect-auth. Everything should be working according to the example middleware in the app.js

@guiomie
Copy link

guiomie commented Aug 20, 2011

The exemple kinda lacks explanation. What does req.authenticate([req.param('method')] in the line req.authenticate([req.param('method')], function(error, authenticated) does? How do you configure properly? I read the facebook doc, but I still cant figure out how to get connect-auth working, authentication failed all the time...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment