From 1330e014844c5d4118c6e8e085358d7af28bb74b Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Sun, 31 Jul 2022 07:15:50 +0300 Subject: [PATCH] working offline accounts --- app/assets/js/authmanager.js | 14 +++++++++++++- app/assets/js/configmanager.js | 13 ++++++++----- app/assets/js/scripts/settings.js | 31 +++++++++++++++++++++++++++++++ app/assets/js/scripts/uibinder.js | 4 +++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/app/assets/js/authmanager.js b/app/assets/js/authmanager.js index 03b3a22..b718fcc 100644 --- a/app/assets/js/authmanager.js +++ b/app/assets/js/authmanager.js @@ -59,7 +59,8 @@ exports.addMojangAccount = async function(username, password) { } exports.addOfflineAccount = async function(usernameOffline){ - ConfigManager.addOfflineAccount('0456456413213', '-', "Megatraher") + //TODO: check for forbidden symbols and lenght!! + ConfigManager.addOfflineAccount(usernameOffline) } const AUTH_MODE = { FULL: 0, MS_REFRESH: 1, MC_REFRESH: 2 } @@ -186,6 +187,17 @@ exports.removeMojangAccount = async function(uuid){ } } +exports.removeOfflineAccount = async function(uuid){ + try { + ConfigManager.removeAuthAccount(uuid) + ConfigManager.save() + return Promise.resolve() + } catch (err){ + log.error('Error while removing account', err) + return Promise.reject(err) + } +} + /** * Remove a Microsoft account. It is expected that the caller will invoke the OAuth logout * through the ipc renderer. diff --git a/app/assets/js/configmanager.js b/app/assets/js/configmanager.js index 4814de6..3e13bba 100644 --- a/app/assets/js/configmanager.js +++ b/app/assets/js/configmanager.js @@ -1,3 +1,4 @@ +const { uuid } = require('discord-rpc-patch/src/util') const fs = require('fs-extra') const os = require('os') const path = require('path') @@ -353,16 +354,18 @@ exports.addMojangAuthAccount = function(uuid, accessToken, username, displayName return config.authenticationDatabase[uuid] } -exports.addOfflineAccount = function(uuid, accessToken, usernameOffline){ - config.selectedAccount = uuid - config.authenticationDatabase[uuid] = { +exports.addOfflineAccount = function(usernameOffline){ + fake_uuid = usernameOffline + config.selectedAccount = fake_uuid + accessToken = "" + config.authenticationDatabase[fake_uuid] = { type: 'offline', accessToken, username: usernameOffline.trim(), - uuid: uuid.trim(), + uuid: fake_uuid.trim(), displayName: usernameOffline.trim() } - return config.authenticationDatabase[uuid] + return config.authenticationDatabase[fake_uuid] } /** * Update the tokens of an authenticated microsoft account. diff --git a/app/assets/js/scripts/settings.js b/app/assets/js/scripts/settings.js index e86de97..96467ad 100644 --- a/app/assets/js/scripts/settings.js +++ b/app/assets/js/scripts/settings.js @@ -331,6 +331,14 @@ document.getElementById('settingsAddMojangAccount').onclick = (e) => { }) } +document.getElementById('settingsAddOfflineAccount').onclick = (e) => { + switchView(getCurrentView(), VIEWS.loginOffline, 500, 500, () => { + loginViewOnCancel = VIEWS.settings + loginViewOnSuccess = VIEWS.settings + loginCancelEnabled(true) + }) +} + // Bind the add microsoft account button. document.getElementById('settingsAddMicrosoftAccount').onclick = (e) => { switchView(getCurrentView(), VIEWS.waiting, 500, 500, () => { @@ -501,6 +509,24 @@ function processLogOut(val, isLastAccount){ switchView(getCurrentView(), VIEWS.waiting, 500, 500, () => { ipcRenderer.send(MSFT_OPCODE.OPEN_LOGOUT, uuid, isLastAccount) }) + } if(targetAcc.type === 'offline') { + AuthManager.removeOfflineAccount(uuid).then(() => { + if(!isLastAccount && uuid === prevSelAcc.uuid){ + const selAcc = ConfigManager.getSelectedAccount() + refreshAuthAccountSelected(selAcc.uuid) + updateSelectedAccount(selAcc) + //validateSelectedAccount() + } + if(isLastAccount) { + loginOptionsCancelEnabled(false) + loginOptionsViewOnLoginSuccess = VIEWS.settings + loginOptionsViewOnLoginCancel = VIEWS.loginOptions + switchView(getCurrentView(), VIEWS.loginOptions) + } + }) + $(parent).fadeOut(250, () => { + parent.remove() + }) } else { AuthManager.removeMojangAccount(uuid).then(() => { if(!isLastAccount && uuid === prevSelAcc.uuid){ @@ -603,6 +629,7 @@ function refreshAuthAccountSelected(uuid){ const settingsCurrentMicrosoftAccounts = document.getElementById('settingsCurrentMicrosoftAccounts') const settingsCurrentMojangAccounts = document.getElementById('settingsCurrentMojangAccounts') +const settingsCurrentOfflineAccounts = document.getElementById('settingsCurrentOfflineAccounts') /** * Add auth account elements for each one stored in the authentication database. @@ -617,6 +644,7 @@ function populateAuthAccounts(){ let microsoftAuthAccountStr = '' let mojangAuthAccountStr = '' + let offlineAuthAccountStr = '' authKeys.forEach((val) => { const acc = authAccounts[val] @@ -647,6 +675,8 @@ function populateAuthAccounts(){ if(acc.type === 'microsoft') { microsoftAuthAccountStr += accHtml + } if (acc.type === 'offline') { + offlineAuthAccountStr += accHtml } else { mojangAuthAccountStr += accHtml } @@ -655,6 +685,7 @@ function populateAuthAccounts(){ settingsCurrentMicrosoftAccounts.innerHTML = microsoftAuthAccountStr settingsCurrentMojangAccounts.innerHTML = mojangAuthAccountStr + settingsCurrentOfflineAccounts.innerHTML = offlineAuthAccountStr } /** diff --git a/app/assets/js/scripts/uibinder.js b/app/assets/js/scripts/uibinder.js index d7b8d7b..c12ae71 100644 --- a/app/assets/js/scripts/uibinder.js +++ b/app/assets/js/scripts/uibinder.js @@ -410,7 +410,9 @@ function setSelectedAccount(uuid){ const authAcc = ConfigManager.setSelectedAccount(uuid) ConfigManager.save() updateSelectedAccount(authAcc) - validateSelectedAccount() + if (ConfigManager.getAuthAccounts[uuid].type !== 'offline'){ + validateSelectedAccount() + } } // Synchronous Listener