Created
September 4, 2011 22:35
-
-
Save ciaranj/1193637 to your computer and use it in GitHub Desktop.
Example connect-auth using scopes to allow con-current strategy authentications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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