From e5c1cd1c29db602512cfdf7379be2dd9da1c22b3 Mon Sep 17 00:00:00 2001 From: Makis Tracend Date: Thu, 10 Jan 2013 20:08:09 -0800 Subject: [PATCH] #36 updating authentication to expect error messages --- lib/client.js | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/client.js b/lib/client.js index a307614..4973eb8 100755 --- a/lib/client.js +++ b/lib/client.js @@ -12,7 +12,7 @@ var crudr, socket; } CRUDr.prototype = { - status : false, + status : "", init : function(){ // defaults @@ -40,6 +40,7 @@ var crudr, socket; */ }, connect: function( options, callback){ + var self = this; // fallback options || (options = {}); // merge with defaults @@ -61,22 +62,22 @@ var crudr, socket; if( typeof options.key != "undefined" && options.auth ) { // get the token... - var cookie = Cookie.get("access_token"); - + var cookie = Cookie.get("crudr_token"); + // if (window.location.hash.length == 0) { //if( typeof( cookie ) == "undefined" && typeof( token ) == "undefined" ){ if( typeof( cookie ) == "undefined" ){ var path = '{{host}}/{{authorize}}'; var params = [ - 'client_id=' + key, + 'client_id=' + options.key, 'redirect_uri=' + this.redirect_uri, 'response_type=token' ]; var url = path + "?" + params.join('&'); //window.location= url; - ajax( url, this.auth ); + ajax( url, function( res ){ self.auth( res ); } ); } else { @@ -118,7 +119,9 @@ var crudr, socket; }); }, state : function( status ){ - if(typeof status != "undefined") { + if( status == "failed" ) this.status = false; + // unless we have failed authenticating the user, continue + if( this.status ) { this.status = status; // load the sockets as soon as possible if(status == "loaded") this.promise.resolve(); @@ -176,23 +179,32 @@ var crudr, socket; CRUDr.prototype.auth = function( response ){ + // settings + var log = this.options.log; try{ - var token = response["access_token"]; - var expiry = response["expires_in"]; - - // save token in cookie... - Cookie.set("access_token", token, expiry); - + + if( response.error ){ + this.state("failed"); + if( log ) this.log(response.message); + } else { + var token = response["access_token"]; + var expiry = response["expires_in"]; + // console.log access token? + // + // save token in cookie... + Cookie.set("crudr_token", token, expiry); + // in any case set the state of the object to: + if( this.status == "loaded" ) { + this.promise.resolve(); + } + + } } catch( e ) { // there was an exception in processing the request - output a default message (for now) - console.log("CRUDr could not be loaded at the present time"); - + console.log("There was an unexpected error loading CRUDr. Please send a report to: http://crudr.com/support"); } - // in any case set the state of the object to: - if( this.status == "loaded" ) { - this.promise.resolve(); - } + /* else { this.state("connected"); }