diff --git a/webui-src/app/login.js b/webui-src/app/login.js index b8bb434..061e0d8 100644 --- a/webui-src/app/login.js +++ b/webui-src/app/login.js @@ -1,19 +1,21 @@ const m = require('mithril'); const rs = require('rswebui'); -const verifyLogin = function(uname, passwd, port) { - let loginHeader = { +const displayErrorMessage = function(message) { + m.render(document.getElementById('error'), message); +}; + +const verifyLogin = async function(uname, passwd, port) { + const loginHeader = { Authorization: 'Basic ' + btoa(uname + ':' + passwd) }; - if (port !== 9092) { - rs.setKeys('', '', port, false); - } + rs.setKeys('', '', port, false); rs.rsJsonApiRequest( '/rsPeers/GetRetroshareInvite', {}, (data, successful) => { if (successful) { - rs.setKeys(uname, passwd); + rs.setKeys(uname, passwd, port); m.route.set('/home'); } else { displayErrorMessage('Incorrect login/password.'); @@ -24,10 +26,6 @@ const verifyLogin = function(uname, passwd, port) { ); }; -const displayErrorMessage = function(message) { - m.render(document.getElementById('error'), message); -}; - const loginComponent = function() { let uname = ''; let passwd = ''; diff --git a/webui-src/app/rswebui.js b/webui-src/app/rswebui.js index 0ff23ab..93408ad 100644 --- a/webui-src/app/rswebui.js +++ b/webui-src/app/rswebui.js @@ -1,13 +1,11 @@ -'use strict'; - -let m = require('mithril'); +const m = require('mithril'); const API_URL = 'http://127.0.0.1:'; let loginKey = { username: '', passwd: '', isVerified: false, - port: 9092, + port: 9092 }; // Make this as object property? @@ -18,52 +16,58 @@ function setKeys(username, password, port = 9092, verified = true) { loginKey.isVerified = verified; } -function rsJsonApiRequest(path, data, callback, async = true, headers = {}, - handleDeserialize, handleSerialize = JSON.stringify) { - // Retroshare will crash if data is not of object type. - data = data || {}; - callback = callback || (() => {}); +function rsJsonApiRequest( + path, + data = {}, + callback = () => {}, + async = true, + headers = {}, + handleDeserialize = JSON.parse, + handleSerialize = JSON.stringify +) { headers['Accept'] = 'application/json'; - if(loginKey.isVerified) { + if (loginKey.isVerified) { headers['Authorization'] = 'Basic ' + btoa(loginKey.username + ':' + loginKey.passwd); } - //console.info('Sending request: \nPath: ', path, '\nData: ', data, - // '\nHeaders:', headers); // NOTE: After upgrading to mithrilv2, options.extract is no longer required // since the status will become part of return value and then // handleDeserialize can also be simply passed as options.deserialize - // TODO: Properly handle types of fail situtations - // Eg. Retroshare switched off, wrong path, incorrect data, etc. - return m.request({ + return m + .request({ method: 'POST', url: API_URL + loginKey.port + path, async, - extract: (xhr) => { + extract: xhr => { // Empty string is not valid json and fails on parse let response = xhr.responseText || '""'; - handleDeserialize = handleDeserialize || JSON.parse; return { status: xhr.status, - body: handleDeserialize(response), + body: handleDeserialize(response) }; }, serialize: handleSerialize, headers: headers, - data: data, + data: data }) - .then((result) => { - if(result.status === 200) { + .then(result => { + if (result.status === 200) { callback(result.body, true); } else { loginKey.isVerified = false; callback(result, false); } + return result; }) .catch(function(e) { callback(e, false); - console.error('Error: While sending request for path:', path, - '\ninfo:', e); + console.error( + 'Error: While sending request for path:', + path, + '\ninfo:', + e + ); + m.route.set('/'); }); } @@ -72,7 +76,7 @@ function setBackgroundTask(task, interval, taskInScope) { // to avoid loss of scope task(); let taskId = setTimeout(function caller() { - if(taskInScope()) { + if (taskInScope()) { task(); taskId = setTimeout(caller, interval); } else { @@ -80,11 +84,10 @@ function setBackgroundTask(task, interval, taskInScope) { } }, interval); return taskId; -}; +} module.exports = { rsJsonApiRequest, setKeys, - setBackgroundTask, + setBackgroundTask }; -