Skip to content

Commit

Permalink
#36 updating authentication to expect error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jan 11, 2013
1 parent a37dc38 commit e5c1cd1
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var crudr, socket;
}

CRUDr.prototype = {
status : false,
status : "",
init : function(){

// defaults
Expand Down Expand Up @@ -40,6 +40,7 @@ var crudr, socket;
*/
},
connect: function( options, callback){
var self = this;
// fallback
options || (options = {});
// merge with defaults
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit e5c1cd1

Please sign in to comment.