Skip to content

Commit

Permalink
#36 Normalizing authentication without prerequisites
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Dec 13, 2012
1 parent 44e9d26 commit a86e02a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 47 deletions.
121 changes: 84 additions & 37 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,30 @@ var crudr, socket;
});
*/
},
connect: function( key, options, callback){
connect: function( options, callback){

options || (options = {});
this.options = options;
this.callback = callback;
this.redirect_uri = window.location;
// get the token...
var cookie = this.getCookie("access_token");

//var query = this.getQuery("access_token");
// set auth state
if( typeof options.auth == "undefined" ) options.auth = true;


// If key is provided expect a valid confirmation from the callback
// unless auth=false, then the key is disegarded...
if( typeof options.key != "undefined" && options.auth ) {

// get the token...
var cookie = this.getCookie("access_token");

if( typeof( options.auth ) != "undefined" && !options.auth ) {
// skip authentication
} else {
// if (window.location.hash.length == 0) {
//if( typeof( cookie ) == "undefined" && typeof( query ) == "undefined" ){
if( typeof( cookie ) == "undefined" ){

var path = '{{host}}/oauth/authorize?';
var path = '{{host}}/{{authorize}}?';
var queryParams = [ 'client_id=' + key,
'redirect_uri=' + this.redirect_uri,
'response_type=token'];
Expand All @@ -60,43 +66,69 @@ var crudr, socket;
//window.location= url;
this.ajaxRequest( url, this.processResponse );

/* deprecated...
} else if( typeof( query ) != "undefined" && query != "false" ) {
// go straight to processing the response
processResponse();
*/

} else {
// continue with the cookie we already have...
//alert(cookie);

}
}

// call callback function
if( this.status == "loaded" && typeof(callback) != "undefined" ) {
callback();
} else {
this.promise.add(callback);

// skip authentication...

// add callback to the promise stack
if( typeof(this.callback) != "undefined" ) {
this.promise.add( this.callback);
}

if( this.status == "loaded" ) {
this.finish();
} else {
this.promise.add( this.finish );
}

}



},
sync : function(socket, req, callbacks) {
socket.emit('sync', req, function(err, resp) {
if (err) {
callbacks.error(err);
} else {
//make sure the response is a JSON;
var data = (typeof resp != "object") ? JSON.parse(resp) : resp;
callbacks.success(data);
}
});
// check if this.status is initialized first...
socket.emit('sync', req, function(err, resp) {
if (err) {
callbacks.error(err);
} else {
//make sure the response is a JSON;
var data = (typeof resp != "object") ? JSON.parse(resp) : resp;
callbacks.success(data);
}
});
},
state : function( status ){
if(typeof status != "undefined") {
this.status = status;
// load the sockets as soon as possible
if(status == "loaded") this.promise.resolve();
}
},
finish: function(){

// instead of refering to the global namespace, find a better way to discover these...
crudr.state("initialized");
crudr.promise.resolve();

// what's this!
// just refresh the page if there's no callback...
/*
if( typeof(this.callback) == "undefined"){
window.location= this.redirect_uri;
} else {
this.callback.call(this, response);
}
*/

}

}

this.Promise = function(obj) {
Expand Down Expand Up @@ -158,6 +190,7 @@ var crudr, socket;
backend.socket.on('synced', function(method, resp) {
var event = backend.options.event;

// create standard events for "dumb" objects
if(typeof collection.trigger == "undefined"){
// fallback to a regular event dispatcher
var evt = document.createEvent("Events");
Expand Down Expand Up @@ -227,16 +260,30 @@ var crudr, socket;


CRUDr.prototype.processResponse = function( response ){
var token = response["access_token"];
var expiry = response["expires_in"];

// save token in cookie...
this.setCookie("access_token", token, expiry);
if( typeof(this.callback) == "undefined"){
window.location= this.redirect_uri;
} else {
this.callback.call(this, response);
try{
var token = response["access_token"];
var expiry = response["expires_in"];

// save token in cookie...
this.setCookie("access_token", token, expiry);
// add the (original) calback in the promise list
if( typeof(this.callback) != "undefined" ) {
this.promise.add( this.callback);
}

} 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");

}
// in any case set the state of the object to:
if( this.status == "loaded" ) {
this.finish();
} else {
this.promise.add( this.finish );
}


}

// create an ajax request
Expand All @@ -248,7 +295,7 @@ var crudr, socket;
req.open("GET",url,true);
req.send(null);
req.onerror = function(){
alert("there was an error with your request");
console.log("there was an error with your request");
};
req.onload = function(e){
var response = JSON.parse(e.target.responseText);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
],
"dependencies": {
"socket.io": ">= 0.8.7",
"underscore": "1.x",
"nconf": ">0.6.x"
"underscore": "1.x"
},
"devDependencies": {
"express": ">= 2.5.0",
Expand Down
20 changes: 12 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
__Open source module for socket-enabled CRUD operations in Node.js__


## Install
## Features

* Authentication compatible with OAuth2 (using middleware)

npm install crudr

## Install
```
npm install crudr
```

## Example
## Usage

On the server:
```
Expand All @@ -23,11 +28,11 @@ On the server:
On the client:
```
<script src="/crudr/client.js"></script>
<script>
crudr.connect( key, options, function(){
// .. initiate app
});
crudr.connect( options, function(){
// .. initiate app
});
</script>
```
Expand All @@ -37,7 +42,6 @@ On the client:
- Socket.io
- Express (only for uri routes)
- Underscore
- Nconf



Expand Down

0 comments on commit a86e02a

Please sign in to comment.