Moving as much auth/initialization logic out of index.html

 New local push function in auth for push server buttons onto the dom
This commit is contained in:
shockrah 2021-03-09 15:36:55 -08:00
parent 911edd8114
commit b5822cd3f1
2 changed files with 25 additions and 40 deletions

View File

@ -114,50 +114,15 @@
(async () => {
const { ipcRenderer } = require('electron')
const auth = require('../src/auth.js')
const channels = require('../src/channels.js')
const { add_server_to_config } = require ('../src/settings.js')
// used for basic styling on server buttons
const classes = 'btn btn-outline-secondary btn-nav-settings'
// NOTE: this will probably take minute if your in a lot of servers
// even worse if server owners are running debug builds like tards
// get a new jwt from each server on startup
$('#add-admin-json').click(async () => await add_server_to_config() )
try {
let config = await auth.init()
for(const server_cfg of config.data['servers']) {
const remote = server_cfg['server']
const user = server_cfg['user']
// html generation
const id = remote['hostname']
const txt = remote['name']
$('#server-list').append(
$('<input>').attr({
type: 'button',
'class' : classes,
id: id,
value: txt
})
)
$(`#${id}`).on('click', async () => await channels.list(remote, user))
}
await auth.init()
} catch(err) {
console.log('Failure in IPC call')
console.log(err)
$('#server-list').append(
$('<input>').attr({
type: 'button',
'class': classes + ' disabled',
value: 'Problems setting up from config'
})
)
}
})()
</script>

View File

@ -2,6 +2,23 @@ const { ipcRenderer } = require('electron')
const got = require('got')
function push_srv(remote, user, enabled) {
let classes = 'btn btn-outline-secondary btn-nav-settings'
if(!enabled) { classes += ' disabled' }
let btn = $('<input>').attr({
type: 'button',
'class': classes,
id: remote['hostname'],
value: remote['name']
})
let cb = async () => { await channels.list(remote, user) }
$(`#${remote['hostname']}`).on('click', cb)
$('#server-list').append(btn)
}
/**
* @param {String} protocol http|https
* @param {String} host Just the hostname one.two.xyz
@ -34,16 +51,19 @@ exports.init = async function() {
const config = await ipcRenderer.invoke('config-request')
for(const index in config.data['servers']) {
console.log('Initial login', config.data['servers'][index])
const remote = config.data['servers'][index]['server']
const user = config.data['servers'][index]['user']
try {
// update the jwt first
const jwt = await login(remote['protocol'], remote['hostname'], remote['port'], user)
config.data['servers'][index]['user']['jwt'] = jwt
user['jwt'] = jwt
push_srv(remote, user, true);
} catch (err) {
continue
push_srv(remote, user, false)
const e_container = { error: err, server: remote, creds: user }
console.log('Failure to communicate with server: ', e_container)
}
}