const { ipcRenderer } = require('electron') const { Request } = require('./request.js') const jsonwebtoken = require('jsonwebtoken') const got = require('got') /** * @param {String} protocol http|https * @param {String} host Just the hostname one.two.xyz * @param {u16} port Port to use * @param {String} user.secret * @param {Number} user.id * */ async function login(protocol, host, port, user) { let pass = false const url = `${protocol}://${host}:${port}/login` const response = await got(url, { searchParams: { id: user['id'], secret: user['secret'] }, responseType: 'json' }) const jwt = response.body['jwt'] let config = await ipcRenderer.invoke('config-request') for(const idx in config['servers']) { // 'tism: once we find the right server conf updated it and jam it back in // to disk, this high level of granularity is ugly but the alternatives // so much worse if(config['servers'][idx]['server']['host'] == host) { config['servers'][idx]['server']['jwt'] = jwt await ipcRenderer.invoke('config-update', config) break } } } exports.login = login /** * @returns {Config} whole config to use in ui building back in DOM-land */ exports.init = async function(config) { for(let conf of config['servers']) { const remote = conf['server'] const user = conf['user'] await login(remote['protocol'], remote['hostname'], remote['port'], user) } }