From 18cb3ac92d88445062056d94ad8e82379b65a2d6 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 25 Mar 2018 13:59:33 +0200 Subject: [PATCH] [page.noauth.login] login page started --- webpack/page/noauth/login.js | 138 +++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/webpack/page/noauth/login.js b/webpack/page/noauth/login.js index 8b13789..39d1efd 100644 --- a/webpack/page/noauth/login.js +++ b/webpack/page/noauth/login.js @@ -1 +1,139 @@ +/* (1) Initialise +---------------------------------------------------------*/ +/* (1) Default data structure */ +gs.set('login', { + // fields + 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){}, + login(){}, + forgot_pass(){} + } + +}); + + + + +/* (2) Manage error messages +* +* @_field Field to print errors to +* @_message Error message to print +* @_duration Durations (sec) for the message to be displayed +* +---------------------------------------------------------*/ +gs.get.login.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.login); + + + + + + + + +/* (3) Login attempt +* +---------------------------------------------------------*/ +gs.get.login.func.login = function(){ + + /* (1) Cache fields' values */ + let username = this.username.model; + let password = this.password.model; + + /* (2) Manage errors */ + let errors = false; + + // 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; + + /* (3) Create API instance */ + let api = new xhrcd('douscord-srv.xdrm.io'); + + /* (4) API bindings */ + api.onreceive = function(_response){ + + // manage error + if( _response.error !== 0 || _response.token == null ) + return gs.get.router.push('register'); + + // manage login + auth.token = _response.token; + document.location = ''; + + }; + + api.onclose = function(){ return gs.get.router.push('register'); }; + + /* (5) API call */ + api.send({ + path: 'POST /user/token', + http_token: encodeURI(`${username}:${password}`) + }); + + + + + + +}.bind(gs.get.login); \ No newline at end of file