Skip to content

Commit

Permalink
fixed msToken expiring and not being refreshed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Davis-Software committed Oct 29, 2022
1 parent 7774142 commit 3dc14e1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
69 changes: 62 additions & 7 deletions back/mc-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {getMainWindow} = require("./electron-tools");
const runningClients = onArrayChange([], () => {
invoke("mc:runningClients", runningClients.length)
})
let mcInstance = null


function afterLaunchCalls(){
Expand All @@ -33,20 +34,65 @@ function askLogin() {
const msMc = new auth('select_account')

return new Promise((resolve) => {
msMc.on('load', console.log).launch('electron').then(async e => {
msMc.launch('electron').then(async e => {
const mc = await e.getMinecraft()
let credentials = mc.mclc()

settings.set("credentials", credentials)
settings.set("refresh-token", e.msToken)

mcInstance = e
resolve(credentials)
}).catch(() => {
mcInstance = null
resolve(null)
})
})
}
function askValidate(){
if(!mcInstance) return false

return mcInstance.validate()
}
function refreshLogin(forceReLogin = false){
if(!settings.get("refresh-token")) return null

const msMc = new auth("none")
return new Promise((resolve) => {
msMc.refresh(settings.get("refresh-token")).then(async e => {
const mc = await e.getMinecraft()
let credentials = mc.mclc()

if(credentials.access_token !== settings.get("credentials").access_token){
settings.set("credentials", credentials)
settings.set("refresh-token", e.msToken)
}

mcInstance = e
resolve(credentials)
}).catch(() => {resolve(null)})
}).catch(() => {
mcInstance = null
if(forceReLogin){
askLogin().then(resolve)
}else{
resolve(null)
}
})
})
}
function logout(){
settings.set("credentials", null)
}

function launchVanilla(version) {
invoke("mc:initGame")

if(!askValidate()){
refreshLogin(true).then(() => launchVanilla(version))
invoke("mc:close", 0)
return
}

const launcher = new Client()
runningClients.push(launcher)

Expand All @@ -69,8 +115,6 @@ function launchVanilla(version) {
}
}

invoke("mc:initGame")

launcher.launch(opts).then(() => {
invoke("mc:gameLaunched")
afterLaunchCalls()
Expand All @@ -93,6 +137,14 @@ function launchVanilla(version) {
}

function launchModded(manifest) {
invoke("mc:initGame")

if(!askValidate()){
refreshLogin(true).then(() => launchModded(manifest))
invoke("mc:close", 0)
return
}

const rootPath = path.join(settings.get("mcPath"), "mod-packs", manifest.id)

let currentManifest
Expand All @@ -113,8 +165,6 @@ function launchModded(manifest) {
installNeeded = true
}

invoke("mc:initGame")

if(installNeeded){
let del = [
"bin",
Expand Down Expand Up @@ -183,10 +233,15 @@ function launchModded(manifest) {
}

registerIpcListener("dialog:askLogin", askLogin)
registerIpcListener("dialog:askValidate", askValidate)
registerIpcListener("dialog:refreshLogin", refreshLogin)
registerIpcListener("dialog:logout", logout)
registerIpcListener("mc:launchVanilla", (e, v) => launchVanilla(v))
registerIpcListener("mc:launchModded", (e, v) => launchModded(v))

module.exports = {
askLogin
askLogin,
askValidate,
refreshLogin,
logout
}
3 changes: 2 additions & 1 deletion back/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const os = require("os");
const {registerIpcListener} = require("./ipc-handler");
const {registerIpcListener, registerIpcListenerSync} = require("./ipc-handler");
const {platform} = require("./config");
const childProcess = require("child_process");
const fs = require("fs");
const path = require("path");


registerIpcListenerSync("log", console.log)
registerIpcListener("get-ram-amount", () => {
return Math.round(os.totalmem() / 1024 / 1024);
})
Expand Down
4 changes: 4 additions & 0 deletions front_src/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LaunchBarPane from "./components/LaunchBarPane";
import LaunchProgressPane from "./components/LaunchProgressPane";
import UpdateInfo from "./components/UpdateInfo";
import GameInfo from "./components/GameInfo";
import {exposedFunctions} from "./utils/constants";


function App(){
Expand All @@ -28,6 +29,9 @@ function App(){

loadModPacks().then(setModPacks)
getSetting("credentials").then(setUserData)
exposedFunctions("dialog").askValidate().then((bool: boolean) => {
if(!bool) exposedFunctions("dialog").refreshLogin().then(setUserData)
})
}, [])

useEffect(() => {
Expand Down
10 changes: 10 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { contextBridge, ipcRenderer } = require("electron")


contextBridge.exposeInMainWorld("utils", {
log: (message) => ipcRenderer.send("log", message),
hashing: {
sha512: (string) => crypto.createHash("sha512").update(string).digest("hex")
},
Expand Down Expand Up @@ -42,6 +43,15 @@ contextBridge.exposeInMainWorld("dialog", {
},
askLogin: () => {
return ipcRenderer.invoke("dialog:askLogin")
},
askValidate: () => {
return ipcRenderer.invoke("dialog:askValidate")
},
refreshLogin: () => {
return ipcRenderer.invoke("dialog:refreshLogin")
},
logout: () => {
return ipcRenderer.invoke("dialog:logout")
}
})
contextBridge.exposeInMainWorld("mc", {
Expand Down

0 comments on commit 3dc14e1

Please sign in to comment.