Skip to content
Permalink
Browse files

Merge pull request #258 from lusbenjamin/auto-login

Auto login and Launch to tray
  • Loading branch information...
Manuel-777 committed Apr 14, 2019
2 parents 04d0179 + 8060515 commit bbb4275ffafddcf8c6f15ce925e27aea6c0460ed
Showing with 177 additions and 101 deletions.
  1. +15 −10 main.js
  2. +3 −0 shared/util.js
  3. +74 −55 window_background/background.js
  4. +6 −2 window_background/http-api.js
  5. +4 −0 window_main/history.js
  6. +75 −34 window_main/renderer.js
25 main.js
@@ -28,12 +28,15 @@ var background;
var overlay;
var tray = null;
var closeToTray = true;
let autoLogin = false;
let launchToTray = false;
var alphaEnabled = false;

const ipc = electron.ipcMain;

var mainLoaded = false;
var backLoaded = false;
var firstSettingsRead = true;

//commandLine, workingDirectory
const singleLock = app.requestSingleInstanceLock();
@@ -129,15 +132,13 @@ function startApp() {
mainWindow.webContents.once("dom-ready", () => {
mainLoaded = true;
if (backLoaded == true) {
showWindow();
background.webContents.send("set_renderer_state", 1);
}
});

background.webContents.once("dom-ready", () => {
backLoaded = true;
if (mainLoaded == true) {
showWindow();
background.webContents.send("set_renderer_state", 1);
}
});
@@ -176,14 +177,17 @@ function startApp() {

case "set_settings":
//console.log("set settings: ", arg);
saveSettings(arg);
setSettings(arg);
mainWindow.webContents.send("set_settings", arg);
overlay.webContents.send("set_settings", arg);
break;

case "set_db":
mainWindow.webContents.send("set_db", arg);
overlay.webContents.send("set_db", arg);
if (autoLogin) {
background.webContents.send("auto_login");
}
break;

case "popup":
@@ -203,12 +207,6 @@ function startApp() {
overlay.webContents.send("set_cards", arg.cards);
break;

case "save_settings":
saveSettings(arg);
background.webContents.send("save_settings", arg);
overlay.webContents.send("set_settings", arg);
break;

case "renderer_update_install":
if (updateState == 3) {
autoUpdater.quitAndInstall();
@@ -382,17 +380,24 @@ function startApp() {
});
}

function saveSettings(settings) {
function setSettings(settings) {
app.setLoginItemSettings({
openAtLogin: settings.startup
});
closeToTray = settings.close_to_tray;
autoLogin = settings.auto_login;
launchToTray = settings.launch_to_tray;

if (!launchToTray && firstSettingsRead) {
showWindow();
}

var oldAlphaEnabled = alphaEnabled;
alphaEnabled = settings.overlay_alpha_back < 1;
if (oldAlphaEnabled != alphaEnabled) {
recreateOverlay();
}
firstSettingsRead = false;
}

// Catch exceptions
@@ -16,6 +16,9 @@ const BLACK = 3;
const RED = 4;
const GREEN = 5;

// Magic constant to represent auth token in form
const HIDDEN_PW = "********";

const math = require("mathjs");
math.config({ precision: 2000 });

@@ -36,7 +36,8 @@ global
onLabelInEventCompleteDraft,
onLabelMatchGameRoomStateChangedEvent,
onLabelInEventGetSeasonAndRankDetail,
onLabelGetPlayerInventoryGetRewardSchedule
onLabelGetPlayerInventoryGetRewardSchedule,
HIDDEN_PW
*/
var electron = require("electron");

@@ -61,7 +62,12 @@ const manifestParser = require("./manifest-parser");

const rememberCfg = {
email: "",
token: ""
token: "",
settings: {
auto_login: false,
launch_to_tray: false,
remember_me: true
}
};

const settingsCfg = {
@@ -260,22 +266,21 @@ function ipc_send(method, arg, to = windowRenderer) {
ipc.send("ipc_switch", method, windowBackground, arg, to);
}

var rememberMe = false;

//
ipc.on("remember", function(event, arg) {
rememberMe = arg;
if (!arg) {
ipc.on("save_app_settings", function(event, arg) {
const rSettings = rstore.get("settings");
const updated = { ...rSettings, ...arg };

if (!updated.remember_me) {
rstore.set("email", "");
rstore.set("token", "");
}
});

function loadSettings() {
var settings = store.get("settings");
updateSettings(settings, true);
}
rstore.set("settings", updated);
loadSettings();
});

//
ipc.on("reload_overlay", function() {
loadSettings();
var obj = store.get("overlayBounds");
@@ -288,23 +293,58 @@ ipc.on("set_renderer_state", function(event, arg) {
renderer_state = arg;
loadSettings();

if (rstore.get("token") !== "" && rstore.get("email") !== "") {
rememberMe = true;
tokenAuth = rstore.get("token");
ipc_send("set_remember", rstore.get("email"));
let username = "";
let password = "";
const remember_me = rstore.get("settings").remember_me;

if (remember_me) {
username = rstore.get("email");
const token = rstore.get("token");
if (username != "" && token != "") {
password = HIDDEN_PW;
tokenAuth = token;
}
}

ipc_send("prefill_auth_form", {
username,
password,
remember_me
});
});

function offlineLogin() {
ipc_send("auth", { ok: true, user: -1 });
ipc_send("set_offline", true);
loadPlayerConfig(playerData.arenaId);
playerData.userName = "";
}

//
ipc.on("auto_login", () => {
const rSettings = rstore.get("settings");
if (!rSettings.auto_login) return;

tokenAuth = rstore.get("token");
ipc_send("popup", {
text: "Logging in automatically...",
time: 0
});
if (rSettings.remember_me) {
httpApi.httpAuth(rstore.get("email"), HIDDEN_PW);
} else {
offlineLogin();
}
});

//
ipc.on("login", function(event, arg) {
if (arg.password == "********") {
if (arg.password == HIDDEN_PW) {
tokenAuth = rstore.get("token");
playerData.userName = arg.username;
httpApi.httpAuth(arg.username, arg.password);
} else if (arg.username == "" && arg.password == "") {
ipc_send("auth", { ok: true, user: -1 });
loadPlayerConfig(playerData.arenaId);
playerData.userName = "";
offlineLogin();
} else {
playerData.userName = arg.username;
tokenAuth = "";
@@ -328,9 +368,9 @@ ipc.on("overlayBounds", function(event, obj) {
});

//
ipc.on("save_settings", function(event, settings) {
ipc.on("save_user_settings", function(event, settings) {
store.set("settings", settings);
updateSettings(settings, false);
loadSettings();
});

//
@@ -635,18 +675,6 @@ function sendEconomy() {
ec.wcMythic = wcMythic;
ipc_send("set_economy", JSON.stringify(ec));
}
/*
function rememberLogin(bool) {
if (bool) {
rstore.set("email", playerData.userName);
rstore.set("token", tokenAuth);
}
else {
rstore.set("email", "");
rstore.set("token", "");
}
}
*/

// Loads this player's configuration file
function loadPlayerConfig(playerId, serverData = undefined) {
@@ -858,39 +886,31 @@ function syncUserData(data) {
store.set("draft_index", draft_index);
}

// Updates the settings variables , sends to overlay if 'relay' is set
function updateSettings(_settings, relay) {
//console.log(_settings);
//const exeName = path.basename(process.execPath);
// Loads and combines settings variables, sends result to display
function loadSettings() {
// Blends together default, user, and app config
// Since settings have migrated between areas, collisions happen
// Order of precedence is: app > user > defaults
const settings = store.get("settings");
const rSettings = rstore.get("settings");
const _settings = { ...defaultCfg.settings, ...settings, ...rSettings };

if (_settings.overlay_top == undefined) _settings.overlay_top = true;
if (_settings.overlay_title == undefined) _settings.overlay_title = true;
if (_settings.overlay_deck == undefined) _settings.overlay_deck = true;
if (_settings.overlay_clock == undefined) _settings.overlay_clock = true;
if (_settings.overlay_ontop == undefined) _settings.overlay_ontop = true;
if (_settings.overlay_scale == undefined) _settings.overlay_scale = 100;
if (_settings.skip_firstpass == undefined) _settings.skip_firstpass = false;
if (_settings.decks_last_used == undefined) _settings.decks_last_used = [];
if (_settings.sound_priority_volume == undefined)
_settings.sound_priority_volume = 1;

//console.log(_settings);
//const exeName = path.basename(process.execPath);

skipFirstPass = _settings.skip_firstpass;

ipc_send("overlay_set_ontop", _settings.overlay_ontop);

if (_settings.export_format == undefined) {
_settings.export_format = "$Name,$Count,$Rarity,$SetName,$Collector";
}

if (_settings.show_overlay == false) {
ipc_send("overlay_close", 1);
} else if (duringMatch || _settings.show_overlay_always) {
ipc_send("overlay_show", 1);
}

if (relay) {
ipc_send("set_settings", _settings);
}
ipc_send("set_settings", _settings);
}

//
@@ -1256,7 +1276,6 @@ function processLogUser(rawString) {
if (value.indexOf(strCheck) > -1) {
playerData.name = dataChop(value, strCheck, '"');
ipc_send("set_player_data", playerData);
ipc_send("init_login", true);
ipc_send("ipc_log", "Arena screen name: " + playerData.name);
}

@@ -2,7 +2,6 @@
global
tokenAuth,
decks,
rememberMe,
rstore,
loadPlayerConfig,
cardsDb,
@@ -90,6 +89,7 @@ function httpBasic() {
_headers.method != "get_status" &&
debugLog == false
) {
ipc_send("set_offline", true);
callback({
message: "Settings dont allow sending data! > " + _headers.method
});
@@ -253,7 +253,7 @@ function httpBasic() {

ipc_send("auth", parsedResult);
//ipc_send("auth", parsedResult.arenaids);
if (rememberMe) {
if (rstore.get("settings").remember_me) {
rstore.set("token", tokenAuth);
rstore.set("email", playerData.userName);
}
@@ -319,6 +319,7 @@ function httpBasic() {
time: 1000
});
ipc_send("set_db", results);
ipc_send("show_login", true);
}
} else if (_headers.method == "tou_join") {
ipc_send("popup", {
@@ -380,6 +381,9 @@ function httpBasic() {
});
});
req.on("error", function(e) {
if (_headers.method == "get_database") {
ipc_send("show_login", true);
}
console.error(`problem with request: ${e.message}`);
if (!metadataState) {
ipc_send("popup", {
@@ -23,6 +23,8 @@ globals
playerData,
tags_colors,
showLoadingBars,
sidebarActive,
decks
$$
*/

@@ -58,6 +60,8 @@ function filterMatch(match) {
}

function open_history_tab(loadMore) {
if (sidebarActive != 1 || decks == null) return;

sort_decks();
var mainDiv = document.getElementById("ux_0");
var div, d;
Oops, something went wrong.

0 comments on commit bbb4275

Please sign in to comment.
You can’t perform that action at this time.