1

When i call this function logout(true) in my back end electron code the dialog box shows before the renderer has finished loading the new page is there anyway i can make this code run in the correct order where the page loads then the error message is sent?

function logout(authFail) {
  win.loadFile(path.join(__dirname, 'src','login.html'));
  currentusername = null
  currentpassword = null
  if (authFail == true) {
    dialog.showErrorBox("Error","Unauthorised Access!")
  }
}
Lucas
  • 598
  • 9
  • 18

2 Answers2

1

win.loadFile is in most cases an async operation, so you have to wait to the login.html to be loaded.

I suppose you can use

win.once('ready-to-show', () => {
  if (authFail == true) {
    dialog.showErrorBox("Error","Unauthorised Access!")
  }
})

https://www.electronjs.org/docs/api/browser-window#using-ready-to-show-event

Andrea Franchini
  • 548
  • 4
  • 14
  • Thank you very much i didn't think about using a ready event however this didn't quite work for me as it appears that ready-to-show is only sent once when the renderer is first loaded and isn't sent again however i was able to get a working solution after you put me on the right track and will add it now – Lucas Feb 13 '20 at 12:48
0

Many thanks to Andrea Franchini's answer for putting me on the right tracks but it appears that ready-to-show is only sent once however after some research i found this solution which worked for me:

function logout(authFail) {
  win.loadFile(path.join(__dirname, 'src','login.html'));
  currentusername = null
  currentpassword = null
  win.webContents.on('did-finish-load', () =>{
    if (authFail == true) {
      dialog.showErrorBox("Error","Unauthorised Access!")
    }
  })
}

Reference: https://stackoverflow.com/a/50980097/12887221

Lucas
  • 598
  • 9
  • 18