I'm looking at implementing a login system in an Electron[0] application which I'm building but getting stuck on the part of handling the session. Basically I want to store the users session so it is persisted between application restarts (if "Remember me" is enabled).
I have to make use of an existing back-end which works with cookie authentication and I'm not able to change anything there.
From the Electron documentation on the Session object[1] I gathered that I should be using a partition like f.e. persist:someName
in order to have a persistent storage, but this is not persisted between application restarts as it seems.
The way I currently set the cookie is as follows:
// main-process/login.js
const session = require('electron').session;
const currentSession = session.fromPartition('persist:someName').cookies;
currentSession.set({
name: 'myCookie',
url: '',
value: 'loggedin=1',
expirationDate: 1531036000
}, function(error) {
console.log('Cookie set');
if (error) {
console.dir(error);
}
});
After running this, I see the Cookie set
output, but when restarting the app and running the following code:
// main.js
const session = require('electron').session;
const currentSession = session.fromPartition('persist:someName').cookies;
currentSession.get({}, function(error, cookies) {
console.dir(cookies);
if (error) {
console.dir(error);
}
});
The output returned is []
.
Any pointers as to what I'm doing wrong or need to do differently would be highly appreciated!
[0]
[1] /
I'm looking at implementing a login system in an Electron[0] application which I'm building but getting stuck on the part of handling the session. Basically I want to store the users session so it is persisted between application restarts (if "Remember me" is enabled).
I have to make use of an existing back-end which works with cookie authentication and I'm not able to change anything there.
From the Electron documentation on the Session object[1] I gathered that I should be using a partition like f.e. persist:someName
in order to have a persistent storage, but this is not persisted between application restarts as it seems.
The way I currently set the cookie is as follows:
// main-process/login.js
const session = require('electron').session;
const currentSession = session.fromPartition('persist:someName').cookies;
currentSession.set({
name: 'myCookie',
url: 'https://www.example.',
value: 'loggedin=1',
expirationDate: 1531036000
}, function(error) {
console.log('Cookie set');
if (error) {
console.dir(error);
}
});
After running this, I see the Cookie set
output, but when restarting the app and running the following code:
// main.js
const session = require('electron').session;
const currentSession = session.fromPartition('persist:someName').cookies;
currentSession.get({}, function(error, cookies) {
console.dir(cookies);
if (error) {
console.dir(error);
}
});
The output returned is []
.
Any pointers as to what I'm doing wrong or need to do differently would be highly appreciated!
[0] http://electron.atom.io
[1] http://electron.atom.io/docs/api/session/
- 2 After looking into the issue some more, it appears this might be an issue with Electron releases after 1.2.2, as pointed out in this thread on GitHub: github./electron/electron/issues/6388 – Revell Commented Jul 8, 2016 at 9:32
- 1.: Check your import, I imagine " require('electron'] " may cause some problems. 2.: You could check out the localStorage API instead. – Jens Habegger Commented Jul 8, 2016 at 12:50
- @JensHabegger Yeah, that's been a typo in the script here, sorry! In the meantime it's turned out that this was an issue in Electron and I'm waiting for a new release with the fix for this to bee available. – Revell Commented Jul 12, 2016 at 11:29
-
In my experience, the session API is full of unexplainable behavior. In my app, I get/set cookies before a window is ever created and in order for the cookies to actually get set, I had to first set them, and then get them all again (like
session.cookies.get({}, () => {})
). Doesn't make sense, but it does consistently work. – ccnokes Commented Sep 7, 2016 at 20:03 - Have you verified that it's not in the window via the Chrome dev tools? (Look under the Application or Resources tab). – ccnokes Commented Sep 7, 2016 at 20:05
1 Answer
Reset to default 1An alternative might be to take a look at electron-json-storage. Using this plugin, you can write JSON to a system file throughout the user experience and then recall that file on the application load to replace the user "state".
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742416453a4439835.html
评论列表(0条)