diff --git a/webpack/lib/api-client.js b/webpack/lib/api-client.js index 7eb7976..848e3bf 100644 --- a/webpack/lib/api-client.js +++ b/webpack/lib/api-client.js @@ -10,9 +10,9 @@ export default class APIClient{ * @return http_url Built http_url * ---------------------------------------------------------*/ - constructor(_hostname, _auth){ + constructor(_hostname){ - this.xhr_driver = new XHRClientDriver(_hostname, _auth); + this.xhr_driver = new XHRClientDriver(_hostname, true); } @@ -36,7 +36,7 @@ export default class APIClient{ * 6. ); * */ - call(_path, _form={}, _callback, _token=null){ + call(_path, _form, _callback, _token=null){ /* (1) Argument management @@ -45,18 +45,22 @@ export default class APIClient{ if( !(_callback instanceof Function) ) _callback = function(r){ console.warn('The API callback function is missing, default callback set.', 'Response', r); }; + var persist = { received: false }; + /* (2) Bind callbacks */ this.xhr_driver.onreceive = function(_response){ + this.received = true; + /* Try to parse JSON */ try{ _response = JSON.parse(_response); }catch(e){ _callback({error: -1}); } /* Launch @_callback with response */ _callback(_response); - } + }.bind(persist); - this.xhr_driver.onclose = function(){ _callback({ error: -2 }); } + this.xhr_driver.onclose = function(){ !this.received && _callback({ error: -2 }); }.bind(persist); return this.xhr_driver.send({ diff --git a/webpack/lib/client/xhr.js b/webpack/lib/client/xhr.js index b0362d7..0ab9326 100644 --- a/webpack/lib/client/xhr.js +++ b/webpack/lib/client/xhr.js @@ -30,7 +30,7 @@ export default class XHRClientDriver extends ClientDriver{ this.resource = _resource.replace(/^https?:\/\//, ''); /* (3) Manage @proto default values */ - this.proto = typeof _auth !== 'object' && _auth['ssl'] != null && _auth.ssl === true; + this.proto = typeof _auth !== 'object' || _auth['ssl'] == null || _auth.ssl !== true; this.proto = (this.proto) ? 'https://' : 'http://'; } @@ -152,13 +152,12 @@ export default class XHRClientDriver extends ClientDriver{ /* (1) Build full request URI */ let request_uri = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/'); - /* (2) Build protocol (http, https) and http_token */ - let protocol = http_token != null ? `${this.proto}${http_token}@` : this.proto; + /* (2) Open connection */ + console.log(`API >>> ${this.proto}${request_uri}`); + this.xhr.open(http_method, `${this.proto}${request_uri}`, true); + this.xhr.setRequestHeader('Authorization', `Basic ${btoa(http_token)}`); - /* (3) Open connection */ - this.xhr.open(http_method, `${protocol}${request_uri}`, true); - - /* (4) Send request */ + /* (3) Send request */ this.xhr.send(form_data); return true; diff --git a/webpack/page/noauth/login.js b/webpack/page/noauth/login.js index 544c3a1..2eb4a7f 100644 --- a/webpack/page/noauth/login.js +++ b/webpack/page/noauth/login.js @@ -108,26 +108,16 @@ gs.get.login.func.login = function(){ return false; /* (3) API bindings */ - api.onreceive = function(_response){ - - _response = JSON.parse(_response); + api.call('GET /user/token', {}, function(rs){ // manage error - if( _response.error !== 0 || _response.token == null ) + if( rs.error !== 0 || rs.token == null ) return gs.get.router.push('register'); // manage login - auth.token = _response.token; + auth.token = rs.token; document.location = ''; - }; - - api.onclose = function(){ return gs.get.router.push('register'); }; - - /* (4) API call */ - api.send({ - path: 'POST /user/token', - http_token: encodeURI(`${username}:${password}`) - }); + }, encodeURI(`${username}:${password}`)); }.bind(gs.get.login); \ No newline at end of file diff --git a/webpack/page/noauth/register.js b/webpack/page/noauth/register.js index d87fff8..b1837c9 100644 --- a/webpack/page/noauth/register.js +++ b/webpack/page/noauth/register.js @@ -126,16 +126,18 @@ gs.get.register.func.register = function(){ /* (3) API bindings */ api.call('POST /user', { username: username, password: password }, function(rs){ + console.log(rs); + // manage error if( rs.error !== 0 || rs.uid == null || rs.token == null ) return gs.get.router.push('register'); // manage login - gstore.set('uid', rs.uid); + gs.set('uid', rs.uid); auth.token = rs.token; document.location = ''; - }, auth.toen); + }); }.bind(gs.get.register); \ No newline at end of file