Skip to content

Instantly share code, notes, and snippets.

@ciaranj
Created September 4, 2011 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciaranj/1193637 to your computer and use it in GitHub Desktop.
Save ciaranj/1193637 to your computer and use it in GitHub Desktop.
Example connect-auth using scopes to allow con-current strategy authentications.
var express= require('express')
, auth= require('./.node_modules/connect-auth')
, url= require('url');
var app = express.createServer()
, yourTwitterConsumerKey= "xxx"
, yourTwitterConsumerSecret= "yyyy"
, fbId= "asdsd"
, fbSecret= "asdsdsds"
, fbCallbackAddress= "http://somehost.com/auth/facebook_callback";
function auth_middleware(){
return function(req, res, next) {
var urlp= url.parse(req.url, true)
if( urlp.query.login_with ) {
req.authenticate([urlp.query.login_with], {scope: urlp.query.login_with}, function(error, authenticated) {
if( error ) {
// Something has gone awry, behave as you wish.
console.log( error );
res.end();
} else {
if( authenticated === undefined ) {
// The authentication strategy requires some more browser interaction, suggest you do nothing here!
}
else {
// We've either failed to authenticate, or succeeded (req.isAuthenticated() will confirm, as will the value of the received argument)
next();
}
}});
} else {
next();
}
}
};
function firstLoginHandler(authContext, executionResult, callback ){
console.log( 'First Login' );
console.log( executionResult );
callback();
}
app.configure(function(){
app.use(express.cookieParser());
app.use(express.session({ secret: 'foobar' }));
app.use(auth({strategies:[ auth.Twitter({consumerKey: yourTwitterConsumerKey, consumerSecret: yourTwitterConsumerSecret}),
auth.Facebook({appId : fbId, appSecret: fbSecret, scope: "email", callback: fbCallbackAddress})],
trace: true,
logoutHandler: require('connect-auth/lib/events').redirectOnLogout("/"),
firstLoginHandler: firstLoginHandler
}));
app.use( auth_middleware() );
});
app.get('/', function(req, res){
res.send('Hello World <a href="/secrets">Secrets!</a>');
});
app.listen(80);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment