2018-03-25 11:59:33 +00:00
|
|
|
/* (1) Initialise
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Default data structure */
|
|
|
|
gs.set('login', {
|
|
|
|
// fields
|
2018-03-28 23:26:18 +00:00
|
|
|
username: new FieldValidator('basic-name', ''),
|
|
|
|
password: new FieldValidator('password', ''),
|
|
|
|
|
|
|
|
// login failed
|
|
|
|
failed: false,
|
|
|
|
|
2018-03-25 11:59:33 +00:00
|
|
|
|
|
|
|
// functions
|
|
|
|
func: {
|
|
|
|
login(){},
|
2018-03-27 17:04:06 +00:00
|
|
|
forgot_pass(){},
|
|
|
|
press_enter(){}
|
2018-03-25 11:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 23:26:18 +00:00
|
|
|
/* (2) Login attempt
|
2018-03-25 11:59:33 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.login.func.login = function(){
|
|
|
|
|
|
|
|
/* (1) Cache fields' values */
|
2018-03-28 23:26:18 +00:00
|
|
|
let username = this.username.mutable;
|
|
|
|
let password = this.password.mutable;
|
2018-03-25 11:59:33 +00:00
|
|
|
|
|
|
|
/* (2) Manage errors */
|
2018-03-28 23:26:18 +00:00
|
|
|
if( !this.username.is_valid() )
|
|
|
|
return false;
|
2018-03-25 11:59:33 +00:00
|
|
|
|
2018-03-28 23:26:18 +00:00
|
|
|
if( !this.password.is_valid() )
|
|
|
|
return false;
|
2018-03-25 11:59:33 +00:00
|
|
|
|
2018-03-27 12:10:00 +00:00
|
|
|
/* (3) API bindings */
|
2018-03-27 14:34:30 +00:00
|
|
|
api.call('GET /user/token', {}, function(rs){
|
2018-03-27 12:52:14 +00:00
|
|
|
|
2018-03-25 11:59:33 +00:00
|
|
|
// manage error
|
2018-03-28 23:26:18 +00:00
|
|
|
if( rs.error !== 0 || rs.token == null )
|
|
|
|
return this.failed = true;
|
2018-03-28 17:42:29 +00:00
|
|
|
|
|
|
|
// store TOKEN + user data
|
|
|
|
auth.token = rs.token;
|
|
|
|
auth.user = {
|
|
|
|
uid: null, // todo
|
|
|
|
username: username
|
|
|
|
};
|
2018-03-25 11:59:33 +00:00
|
|
|
document.location = '';
|
|
|
|
|
2018-03-28 17:42:29 +00:00
|
|
|
}.bind(this), encodeURI(`${username}:${password}`));
|
2018-03-25 11:59:33 +00:00
|
|
|
|
2018-03-27 17:04:06 +00:00
|
|
|
}.bind(gs.get.login);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Manage pressing on enter
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.login.func.press_enter = function(e){
|
|
|
|
|
|
|
|
// if enter -> launch login
|
|
|
|
if( e.keyCode === 13 )
|
|
|
|
this.func.login();
|
|
|
|
|
2018-03-25 11:59:33 +00:00
|
|
|
}.bind(gs.get.login);
|