Permalink
Browse files

Merged changes from staging server

  • Loading branch information...
xrd committed Jan 4, 2011
1 parent f9848f7 commit ae318016712095e1702a895b4ce9b31ebd17da67
Showing with 17 additions and 10 deletions.
  1. +17 −10 lib/connect-rpx.js
View
@@ -40,6 +40,7 @@ function getCredentials(req,res,next) {
postWithCredentials( token, req, res, next );
}
+var rpxResponseBody = '';
function postWithCredentials( token, req, res, next ) {
var apiKey = options['apiKey'];
var toPost = qs.stringify( { token : token, apiKey : apiKey } );
@@ -49,26 +50,32 @@ function postWithCredentials( token, req, res, next ) {
var postRequest = http.createClient( 443, RPX_HOST, true ).request( 'POST', RPX_LOGIN_ROOT, toPostHeader );
postRequest.write( toPost, 'utf8' );
postRequest.on( 'response', function(rpxResponse) {
- var rpxResponseBody = '';
- rpxResponse.on( 'data', function(chunk) { rpxResponseBody += chunk; } );
- rpxResponse.on( 'end', onCredentialsReceived( rpxResponseBody, req, res, next ) );
- rpxResponse.on( 'error', onError );
+ rpxResponse.on( 'data', chunkRpxResponse );
+ rpxResponse.on( 'end', function() { onCredentialsReceived( rpxResponseBody, req, res, next ) } );
+ rpxResponse.on( 'error', onError );
});
postRequest.end();
}
+function chunkRpxResponse( chunk ) {
+ rpxResponseBody += chunk;
+}
+
function onError(response) {
sys.puts( "Something bad happened" );
}
function onCredentialsReceived(data, req, res, next) {
- try {
- json = JSON.parse( data );
- }
- catch( e ) {
- sys.puts( "E: " + sys.inspect( e ) );
+ var json;
+ if( data ) {
+ try {
+ json = JSON.parse( data );
+ }
+ catch( e ) {
+ sys.puts( "E: " + sys.inspect( e ) );
+ }
}
- if( 'ok' == json.stat ) {
+ if( json && 'ok' == json.stat ) {
options.onSuccessfulLogin( json, req, res, next );
}
else {

0 comments on commit ae31801

Please sign in to comment.