2018-03-25 12:49:59 +00:00
|
|
|
/* (1) Initialise
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Default data structure */
|
|
|
|
gs.set('register', {
|
|
|
|
// fields
|
|
|
|
mail: {
|
|
|
|
model: '',
|
|
|
|
timeout: '',
|
|
|
|
error: '',
|
2018-03-27 12:30:04 +00:00
|
|
|
validate: (_mail) => true
|
|
|
|
// validate: (_mail) => /^[\w\.]+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/i.test(_mail)
|
2018-03-25 12:49:59 +00:00
|
|
|
},
|
2018-03-25 10:31:05 +00:00
|
|
|
|
2018-03-25 12:49:59 +00:00
|
|
|
username: {
|
|
|
|
model: '',
|
|
|
|
timeout: '',
|
|
|
|
error: '',
|
|
|
|
validate: (_username) => /^[a-z0-9_-]{3,20}$/i.test(_username)
|
|
|
|
},
|
|
|
|
|
|
|
|
password: {
|
|
|
|
model: '',
|
|
|
|
timeout: '',
|
|
|
|
error: '',
|
|
|
|
validate: (_password) => /^[^<>\/\\]{8,50}$/.test(_password)
|
|
|
|
},
|
|
|
|
|
|
|
|
// functions
|
|
|
|
func: {
|
|
|
|
print_err(_field, _message, _duration){},
|
2018-03-27 17:04:06 +00:00
|
|
|
register(){},
|
|
|
|
press_enter(){}
|
2018-03-25 12:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Manage error messages
|
|
|
|
*
|
|
|
|
* @_field<String> Field to print errors to
|
|
|
|
* @_message<String> Error message to print
|
|
|
|
* @_duration<int> Durations (sec) for the message to be displayed
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.register.func.print_err = function(_field, _message='error', _duration=null){
|
|
|
|
|
|
|
|
/* (1) Fail: invalid _field */
|
|
|
|
if( typeof _field !== 'string' || this[_field] == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Fail: invalid _message */
|
|
|
|
if( typeof _message !== 'string' )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Fail: invalid _message */
|
|
|
|
if( isNaN(_duration) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (4) Clear timeout if exists */
|
|
|
|
!isNaN(this[_field].err_to) && clearTimeout(this[_field].err_to);
|
|
|
|
|
|
|
|
/* (5) Display error */
|
|
|
|
this[_field].error = _message;
|
|
|
|
|
|
|
|
/* (6) No timeout if _duration if null */
|
|
|
|
if( _duration === null )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* (7) Setup Timeout */
|
|
|
|
this[_field].err_to = setTimeout( function(){
|
|
|
|
this.error = '';
|
|
|
|
}.bind(this[_field]), _duration*1000);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}.bind(gs.get.register);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Login attempt
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.register.func.register = function(){
|
|
|
|
|
|
|
|
/* (1) Cache fields' values */
|
|
|
|
let mail = this.mail.model;
|
|
|
|
let username = this.username.model;
|
|
|
|
let password = this.password.model;
|
|
|
|
|
|
|
|
/* (2) Manage errors */
|
|
|
|
let errors = false;
|
|
|
|
|
|
|
|
// mail error
|
|
|
|
if( !this.mail.validate(mail) ){
|
|
|
|
errors = true;
|
|
|
|
this.func.print_err('mail', 'This field is required');
|
|
|
|
}else
|
|
|
|
this.func.print_err('mail', '', 0);
|
|
|
|
|
|
|
|
// username error
|
|
|
|
if( !this.username.validate(username) ){
|
|
|
|
errors = true;
|
|
|
|
this.func.print_err('username', 'This field is required');
|
|
|
|
}else
|
|
|
|
this.func.print_err('username', '', 0);
|
|
|
|
|
|
|
|
|
|
|
|
// password error
|
|
|
|
if( !this.password.validate(password) ){
|
|
|
|
errors = true;
|
|
|
|
this.func.print_err('password', 'This field is required');
|
|
|
|
}else
|
|
|
|
this.func.print_err('password', '', 0);
|
|
|
|
|
|
|
|
// if errors -> fail
|
|
|
|
if( errors )
|
|
|
|
return false;
|
|
|
|
|
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 14:34:30 +00:00
|
|
|
gs.set('uid', rs.uid);
|
2018-03-27 12:52:14 +00:00
|
|
|
auth.token = rs.token;
|
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);
|