Skip to content

Instantly share code, notes, and snippets.

@cianclarke
Created June 30, 2015 09:56
Show Gist options
  • Save cianclarke/8de794a7e30ecc84c939 to your computer and use it in GitHub Desktop.
Save cianclarke/8de794a7e30ecc84c939 to your computer and use it in GitHub Desktop.
var fh = fh || {};
fh.oauth = function(url, cb){
var browser = window.open(url, '_blank', 'location=yes');
browser.addEventListener('loadstop', function(e){
if (e.url.indexOf('/authed')>-1){
browser.executeScript({
code: "JSON.parse(document.body.querySelector('pre').textContent)"
}, function(result) {
// Result will be required oAuth creds
browser.close();
if (!result || result.length === 0 || !result[0].passport || !result[0].passport.user){
return cb('Could not find authentication token details on response');
}
return cb(null, result[0].passport.user)
});
}
if (e.url.indexOf('/error')>-1){
browser.close();
return cb('Error authenticating you over oAuth')
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment