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