From 95c93c03d7353b418fde42541d0e7e888b4f1e1c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 5 Mar 2014 11:02:47 -0500 Subject: [PATCH] refactor(auth) authenticate --- lib/server/auth.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/server/auth.js b/lib/server/auth.js index 8c74b072..3a3c9d9b 100644 --- a/lib/server/auth.js +++ b/lib/server/auth.js @@ -18,6 +18,7 @@ https = main.https, qs = main.querystring, + pipe = main.pipe, Modules = main.modules, Util = main.util, @@ -48,7 +49,7 @@ }; function authenticate(code, callback) { - var body, req, + var req, storage = Util.findObjByNameInArr(Modules, 'storage'), github = Util.findObjByNameInArr(storage, 'GitHub'), @@ -67,19 +68,17 @@ Util.log(clientId, clientSecret, data); - GithubAuth.headers = { 'content-length': data.length }; - - body = "", - req = https.request(GithubAuth, function(res) { - res.setEncoding('utf8'); - res.on('data', function (chunk) { body += chunk; }); - res.on('end', function() { - Util.exec(pCallBack, qs.parse(body).access_token); + GithubAuth.headers = { 'content-length': data.length }; + + req = https.request(GithubAuth, function(res) { + pipe.getBody(res, function(body) { + Util.exec(callback, qs.parse(body).access_token); }); }); req.write(data); req.end(); + req.on('error', function(e) { Util.exec(callback, e.message); });