login + register works
This commit is contained in:
parent
5fcd1e363b
commit
73a8d1c219
|
@ -10,9 +10,9 @@ export default class APIClient{
|
|||
* @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. );
|
||||
*
|
||||
*/
|
||||
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({
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
|
@ -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);
|
Loading…
Reference in New Issue