login + register works

This commit is contained in:
xdrm-brackets 2018-03-27 16:34:30 +02:00
parent 5fcd1e363b
commit 73a8d1c219
4 changed files with 23 additions and 28 deletions

View File

@ -10,9 +10,9 @@ export default class APIClient{
* @return http_url<String> Built http_url * @return http_url<String> 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. ); * 6. );
* *
*/ */
call(_path, _form={}, _callback, _token=null){ call(_path, _form, _callback, _token=null){
/* (1) Argument management /* (1) Argument management
@ -45,18 +45,22 @@ export default class APIClient{
if( !(_callback instanceof Function) ) if( !(_callback instanceof Function) )
_callback = function(r){ console.warn('The API callback function is missing, default callback set.', 'Response', r); }; _callback = function(r){ console.warn('The API callback function is missing, default callback set.', 'Response', r); };
var persist = { received: false };
/* (2) Bind callbacks */ /* (2) Bind callbacks */
this.xhr_driver.onreceive = function(_response){ this.xhr_driver.onreceive = function(_response){
this.received = true;
/* Try to parse JSON */ /* Try to parse JSON */
try{ _response = JSON.parse(_response); }catch(e){ _callback({error: -1}); } try{ _response = JSON.parse(_response); }catch(e){ _callback({error: -1}); }
/* Launch @_callback with response */ /* Launch @_callback with response */
_callback(_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({ return this.xhr_driver.send({

View File

@ -30,7 +30,7 @@ export default class XHRClientDriver extends ClientDriver{
this.resource = _resource.replace(/^https?:\/\//, ''); this.resource = _resource.replace(/^https?:\/\//, '');
/* (3) Manage @proto default values */ /* (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://'; this.proto = (this.proto) ? 'https://' : 'http://';
} }
@ -152,13 +152,12 @@ export default class XHRClientDriver extends ClientDriver{
/* (1) Build full request URI */ /* (1) Build full request URI */
let request_uri = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/'); let request_uri = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/');
/* (2) Build protocol (http, https) and http_token */ /* (2) Open connection */
let protocol = http_token != null ? `${this.proto}${http_token}@` : this.proto; 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 */ /* (3) Send request */
this.xhr.open(http_method, `${protocol}${request_uri}`, true);
/* (4) Send request */
this.xhr.send(form_data); this.xhr.send(form_data);
return true; return true;

View File

@ -108,26 +108,16 @@ gs.get.login.func.login = function(){
return false; return false;
/* (3) API bindings */ /* (3) API bindings */
api.onreceive = function(_response){ api.call('GET /user/token', {}, function(rs){
_response = JSON.parse(_response);
// manage error // manage error
if( _response.error !== 0 || _response.token == null ) if( rs.error !== 0 || rs.token == null )
return gs.get.router.push('register'); return gs.get.router.push('register');
// manage login // manage login
auth.token = _response.token; auth.token = rs.token;
document.location = ''; document.location = '';
}; }, encodeURI(`${username}:${password}`));
api.onclose = function(){ return gs.get.router.push('register'); };
/* (4) API call */
api.send({
path: 'POST /user/token',
http_token: encodeURI(`${username}:${password}`)
});
}.bind(gs.get.login); }.bind(gs.get.login);

View File

@ -126,16 +126,18 @@ gs.get.register.func.register = function(){
/* (3) API bindings */ /* (3) API bindings */
api.call('POST /user', { username: username, password: password }, function(rs){ api.call('POST /user', { username: username, password: password }, function(rs){
console.log(rs);
// manage error // manage error
if( rs.error !== 0 || rs.uid == null || rs.token == null ) if( rs.error !== 0 || rs.uid == null || rs.token == null )
return gs.get.router.push('register'); return gs.get.router.push('register');
// manage login // manage login
gstore.set('uid', rs.uid); gs.set('uid', rs.uid);
auth.token = rs.token; auth.token = rs.token;
document.location = ''; document.location = '';
}, auth.toen); });
}.bind(gs.get.register); }.bind(gs.get.register);