Skip to content

Instantly share code, notes, and snippets.

@doppiomacchiatto
Forked from redryan/sf_oauth_routes
Last active August 29, 2015 14:23
Show Gist options
  • Save doppiomacchiatto/ecaddb6a389d33045859 to your computer and use it in GitHub Desktop.
Save doppiomacchiatto/ecaddb6a389d33045859 to your computer and use it in GitHub Desktop.
/* SF OAuth request, redirect to SF login */
app.get('/oauth/auth', function(req, res) {
res.redirect(oauth2.getAuthorizationUrl({scope: 'api id web'}));
});
/* OAuth callback from SF, pass received auth code and get access token */
app.get('/oauth/callback', function(req, res) {
var conn = new jsforce.Connection({oauth2: oauth2});
var code = req.query.code;
conn.authorize(code, function(err, userInfo) {
if (err) { return console.error(err); }
console.log('Access Token: ' + conn.accessToken);
console.log('Instance URL: ' + conn.instanceUrl);
console.log('User ID: ' + userInfo.id);
console.log('Org ID: ' + userInfo.organizationId);
req.session.accessToken = conn.accessToken;
req.session.instanceUrl = conn.instanceUrl;
res.redirect('/accounts');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment