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

90 lines
1.6 KiB
JavaScript

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