discord-client/webpack/page/noauth/login.js

76 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-03-25 11:59:33 +00:00
/* (1) Initialise
---------------------------------------------------------*/
/* (1) Default data structure */
gs.set('login', {
// fields
username: new FieldValidator('basic-name', ''),
password: new FieldValidator('password', ''),
// login failed
failed: false,
2018-03-25 11:59:33 +00:00
// functions
func: {
login(){},
forgot_pass(){},
press_enter(){}
2018-03-25 11:59:33 +00:00
}
});
/* (2) Login attempt
2018-03-25 11:59:33 +00:00
*
---------------------------------------------------------*/
gs.get.login.func.login = function(){
/* (1) Cache fields' values */
let username = this.username.mutable;
let password = this.password.mutable;
2018-03-25 11:59:33 +00:00
/* (2) Manage errors */
if( !this.username.is_valid() )
return false;
2018-03-25 11:59:33 +00:00
if( !this.password.is_valid() )
return false;
2018-03-25 11:59:33 +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
if( rs.error !== 0 || rs.token == null )
return this.failed = true;
// 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 = '';
}.bind(this), encodeURI(`${username}:${password}`));
2018-03-25 11:59:33 +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);