const ipcRenderer = require('electron') const { Request } = require('./request.js') const jsonwebtoken = require('jsonwebtoken') /** * Checks to see if the jwt is not expired basically * * @param {String} jwt token string to verify * @returns Boolean */ exports.old_jwt = function (jwt) { const result = jsonwebtoken.decode(jwt) if(result) { const now = Math.floor(Date.now() / 1000) return result['exp'] > now } else { return false; } } /** * @param {Object} id * */ exports.request_new_token = function(server) { const domain = server['domain'] const id = server['id'] const secret = server['secret'] const port = server['port'] Request('post', domain, port, '/login', {id: id, secret: secret}).then( response => { if(response.status_code == 200) { const body = JSON.parse(response.body) server['jwt'] = body // finally notify the ipc of the config change ipcRenderer.sendSync('config-update', server) } }, reason => { console.log('Failure to login: ', reason) } ) }