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

83 lines
1.4 KiB
JavaScript

export default new Promise( (res, rej) => {
/* (1) Initialise
---------------------------------------------------------*/
/* (1) Default data structure */
gs.set('login', {
// fields
username: new FieldValidator('basic-name', ''),
password: new FieldValidator('password', ''),
// login failed
failed: false,
// functions
func: {
login(){},
forgot_pass(){},
press_enter(){}
}
});
/* (2) Login attempt
*
---------------------------------------------------------*/
gs.get.login.func.login = function(){
/* (1) Cache fields' values */
let username = this.username.mutable;
let password = this.password.mutable;
/* (2) Manage errors */
if( !this.username.is_valid() )
return false;
if( !this.password.is_valid() )
return false;
/* (3) API bindings */
api.call('GET /user/token', {}, function(rs){
// manage error
if( rs.error !== 0 || rs.token == null || rs.uid == null )
return this.failed = true;
// store TOKEN + user data
auth.token = rs.token;
auth.user = {
uid: rs.uid,
username: username
};
document.location = '';
}.bind(this), encodeURI(`${username}:${password}`));
}.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();
}.bind(gs.get.login);
res();
});