Created
January 31, 2012 21:09
-
-
Save joshbirk/1712931 to your computer and use it in GitHub Desktop.
Basic Passport / Force.com / Node.js example
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 port = process.env.PORT || 3000; | |
var fs = require('fs'); | |
var express = require('express'); | |
var passport = require('passport') | |
, OAuthStrategy = require('passport-oauth').OAuth2Strategy; | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
passport.use('provider', new OAuthStrategy({ | |
tokenURL: 'https://login.salesforce.com/services/oauth2/token', | |
authorizationURL: 'https://login.salesforce.com/services/oauth2/authorize', | |
clientID: '{YOURCONSUMERKEY}', | |
clientSecret: '{YOURPRIVATEKEY}', | |
callbackURL: 'http://localhost:'+port+'/token' | |
}, | |
function(token, tokenSecret, profile, done) { | |
return done(null, {id: token}); | |
} | |
)); | |
var app = express.createServer(); | |
app.configure(function() { | |
app.use(express.logger()); | |
app.use(express.cookieParser()); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.session({ secret: 'thissecretrocks' })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(app.router); | |
}); | |
app.get('/', | |
function(req, res) { | |
res.send('Hello World.'); | |
}); | |
app.get('/login', passport.authenticate('provider'), | |
function(req, res){ | |
// The request will be redirected to Google for authentication, so this | |
// function will not be called. | |
}); | |
app.get('/token', passport.authenticate('provider', { failureRedirect: '/error' }), | |
function(req, res){ | |
res.send('Logged In.'); | |
}); | |
app.get('/error', function(req, res){ | |
res.send('An error has occured.'); | |
}); | |
app.listen(port, function() { | |
console.log("Listening on " + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment