As an electron client, is there a way I can make session persist after an app close?

淺唱寂寞╮ 提交于 2021-02-19 05:35:23

问题


I have a couple of sites that I visit regularly. In fact, so much so, that I like to have a small electron app on my machine to check for updates (since the site doesn't support email updates or anything of that nature). My script basically just launches a browser, retrieves web content for the site, logs it to a file, and then checks for differences. It then notifies me of a difference, if there is one.

The catch, though, is that while a given browser window is open (even if I close the individual tabs), I can go to these sites and my session information is transferred just fine without me having to login. However, if I close the browser window, then it requires me to login again (usually with some indication that my session timed out).

Surprisingly, this behavior does not happen if I navigate to the site(s) on my mobile browser (Firefox for Android). That is, I can close and reopen the browser as many times as I want, and I'll still be logged in.

It seems to me to be something that isn't being correctly persisted in the cookie database on my local machine. On my desktop, this happens with both Chrome and Mozilla Firefox (I haven't tried other browsers, but I probably could).

So, my question is - is this something I could change by manipulating the cookies in some way to make the application not delete the cookies (or send the correct ones to enable the persistence of login), or is this something that can be controlled server-side only?

(By the way, I'm not talking about closing the browser window and reopening it some time later, I'm talking about closing the browser window and immediately reopening it, so it seems unlikely that the session would have expired on the server-side, unless the server is getting some message from the browser that the browser is being closed, which also seems unlikely).

Edit

I suspect I must be doing something incorrectly, because I just found out that Chrome and Firefox actually do preserve the session information across browser closes. The problem was that, since I was logging in from different clients, it was invalidating my session(s) server-side apparently.

So, I guess now my question is more along the lines of "what am I doing incorrectly that causes the session data to not be correctly stored across electron app instantiations?

My configuration for the BrowserWindow is here:

  const { BrowserWindow } = require('electron').remote;

  let win = new BrowserWindow({width: 800,
    height: 600,
    webPreferences: {
      partition: 'persist:sitePoller'
    }
  });
  win.loadURL('https://site-i-want-to-visit');
});

}

Also, this appears to work just fine for a site like http://www.github.com, but not for an ASP-based site, which seems to be the thing all of the sites that I want to work with (and can't) have in common.

来源:https://stackoverflow.com/questions/40749593/as-an-electron-client-is-there-a-way-i-can-make-session-persist-after-an-app-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!