2018-04-07 14:11:59 +00:00
|
|
|
export default new Promise( (res, rej) => {
|
|
|
|
|
|
|
|
/* (1) Initialise
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Default data structure */
|
|
|
|
gs.set('register', {
|
|
|
|
// fields
|
|
|
|
mail: new FieldValidator('bypass', ''),
|
|
|
|
username: new FieldValidator('basic-name', ''),
|
|
|
|
password: new FieldValidator('password', ''),
|
|
|
|
|
|
|
|
// functions
|
|
|
|
func: {
|
|
|
|
register(){},
|
|
|
|
press_enter(){}
|
|
|
|
}
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
});
|
2018-03-25 12:49:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
/* (2) Login attempt
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.register.func.register = function(){
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
/* (1) Cache fields' values */
|
|
|
|
let mail = this.mail.mutable;
|
|
|
|
let username = this.username.mutable;
|
|
|
|
let password = this.password.mutable;
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
/* (2) Manage errors */
|
|
|
|
// mail error
|
|
|
|
if( !this.mail.is_valid() )
|
|
|
|
return false;
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
// username error
|
|
|
|
if( !this.username.is_valid() )
|
|
|
|
return false;
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
// password error
|
|
|
|
if( !this.password.is_valid() )
|
|
|
|
return false;
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
/* (3) API bindings */
|
|
|
|
api.call('POST /user', { username: username, password: password }, function(rs){
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
// manage error
|
|
|
|
if( rs.error !== 0 || rs.uid == null || rs.token == null )
|
|
|
|
return gs.get.router.push('register');
|
2018-03-27 12:30:04 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
// manage login
|
|
|
|
auth.token = rs.token;
|
|
|
|
auth.user = {
|
|
|
|
uid: rs.uid,
|
|
|
|
username: username
|
|
|
|
};
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
document.location = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}.bind(gs.get.register);
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-03-27 12:52:14 +00:00
|
|
|
|
2018-03-25 12:49:59 +00:00
|
|
|
|
2018-03-27 17:04:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
/* (4) Manage pressing on enter
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.register.func.press_enter = function(e){
|
2018-03-27 17:04:06 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
// if enter -> launch register
|
|
|
|
if( e.keyCode === 13 )
|
|
|
|
this.func.register();
|
2018-03-27 17:04:06 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
}.bind(gs.get.register);
|
2018-03-27 17:04:06 +00:00
|
|
|
|
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
res();
|
2018-03-27 17:04:06 +00:00
|
|
|
|
2018-04-07 14:11:59 +00:00
|
|
|
});
|