jvolker / miq-nz-booking-tools

Tools to help booking a spot in New Zealand Managed Isolation and Quarantine. Automatically refreshes, checks and fills forms in the website to find and book the rare spots.
MIT License
83 stars 35 forks source link

logged out of the MIQ website after X hours (stops the script) #91

Open BananaHammock743 opened 3 years ago

BananaHammock743 commented 3 years ago

Describe your problem Has anyone else started to be logged out of the MIQ website after X hours (which obviously causes the script to stop running)? Yesterday I came back to my computer and I'd been logged out, with no error message, just the login page showing again.

This morning the same thing happened at approx 5:50pm (NZ time)

I have checked with my partner (the only other person with our login details) and they had not logged in recently which would have caused the expected "single session" logout.

To clarify: I'm not saying this is a miq-nz-booking-tools issue, jut something to be aware of if the MIQ website has started logging users out after X hours. This could also be a local computer issue only I'm experiencing.

Steps yo reproduce

  1. Using v0.10.2.exe enter detail (dates, etc) into the app and login to the MIQ website
  2. Website is refreshed as expected
  3. Wait X hours
  4. Come back to computer and see that I've been logged out of the MIQ website

Steps you have taken so far Turned off screensaver completely Turned off screen from turning off after X min Turned off sleep after X min Network adapter settings > Power Management > "Allow the computer to turn off this device to save power" > disabled

Your system (please complete the following information): Win10, using miq-nz-booking-assistance-Windows-v0.10.2.exe with Chromium browser

BananaHammock743 commented 3 years ago

Saw this prompt when I got up in the middle of the night to check.

Could be related to the website update braking the script, and then because of inactivity in the page it showed up. log out prompt

alexDrinkwater commented 3 years ago

I'm seeing this too after about 1.5 hours. This might be a new security feature.

nikhil-bhandari commented 3 years ago

Hi,

Thanks for maintaining this script. This is what I used to detect and avoid auto logouts.

What this does is detect the modal and then clicks on "OK".


const selectors = {
  logout: {
    dialog: {
      window: "#session-expire-time-dialog.dialog-open",
      button: ".btn.btn-secondary"
    },
  }
};

// returns boolean
async function hasLogoutDialog(page) {
  return page.evaluate((selectors) => {
    const {window} = selectors;

    const logoutDialog = document.querySelector(window)

    console.log(!!logoutDialog);

    return !!logoutDialog;
  }, selectors.logout.dialog);
}

Main Function that handles logout dialog

async function handleAutoLogout(page) {
  const hasLogout = await hasLogoutDialog(page);

  if (!hasLogout) {
    return;
  }

  console.log("Found logout dialog.");

  const redirectUrl = await page.url();
  console.log("Handling logout.");

  await page.evaluate((selectors) => {
    setInterval(handleAutoLogout, 1000);

    function handleAutoLogout() {
      const {window, button} = selectors;

      const logoutDialog = document.querySelector(window);

      if (logoutDialog) {
        document.querySelector(`${window} ${button}`).click();
      }
    }
  }, selectors.logout.dialog);

  await page.waitForNavigation({waitUntil: ["networkidle0", "domcontentloaded"], timeout: 0})

  console.log("Redirecting to following url: ", redirectUrl);

  await page.goto(redirectUrl);
}

And here is the code to trigger auto logout for testing purposes. (Yes! there is a span that is clicked programatically to show the Auto Logout modal on miq's website :| )

document.getElementById("session-expire-time").click()

Usage


await handleAutoLogout();
alexDrinkwater commented 3 years ago

@nikhil-bhandari Thanks! Are you willing to create a pull request with those changes?