Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Possibility to persist session cookies #9995

Closed
rophy opened this Issue Jul 13, 2017 · 8 comments

Comments

Projects
None yet
7 participants
@rophy
Copy link

rophy commented Jul 13, 2017

  • Electron version: 1.6.2
  • Operating system: win

Currently, if I close an electron app, all session cookies are lost.
I'd like to have the possibility to keep session cookies. I thought this can be done by using "persist:" partition, but it seems persistent partition has nothing to do with preserving session cookies.

I tried these:

  1. Using partition
new BrowserWindow({
  webPreferences: {
    partition: 'persist:mywin'
  }
})
  1. Using session with explicitly passed partition
var session = electron.session.fromPartition('persist:mywin');

new BrowserWindow({
  webPreferences: {
    session: session
  }
})

Note: "session cookie" are those with cookie.session = true. They're supposed to be cleared on closing browser, but sometimes it can be useful to keep session cookies. For example, Chrome's Continue where you left off.

@MarshallOfSound

This comment has been minimized.

Copy link
Member

MarshallOfSound commented Jul 13, 2017

If you want to do this you can manually preserve and then restore cookies through the session.cookies API. Would this work for your use case?

@rophy

This comment has been minimized.

Copy link
Author

rophy commented Jul 13, 2017

Yes, below is the workaround I am trying to apply right now, which changes session cookie to persistent cookie with expiration date of 2 weeks:

var cookies = electron.session.defaultSession.cookies;
cookies.on('changed', function(event, cookie, cause, removed) {
	if (cookie.session && !removed) {
		var url = util.format('%s://%s%s', (!cookie.httpOnly && cookie.secure) ? 'https' : 'http', cookie.domain, cookie.path);
		console.log('url', url);
		cookies.set({
			url: url,
			name: cookie.name,
			value: cookie.value,
			domain: cookie.domain,
			path: cookie.path,
			secure: cookie.secure,
			httpOnly: cookie.httpOnly,
			expirationDate: Math.floor(new Date().getTime()/1000)+1209600
		}, function(err) {
			if (err) {
				log.error('Error trying to persist cookie', err, cookie);
			}
		});
	}
});

Still, it'll be really nice if this managed by electron, so we don't need to modify cookies from external sources.

@onassar

This comment has been minimized.

Copy link

onassar commented Apr 4, 2018

@rophy what is the util object you're using?

@codebytere

This comment has been minimized.

Copy link
Member

codebytere commented Sep 24, 2018

Given that this isn't currently on our roadmap, i'm going to go ahead and label this a wontfix. However, should someone determine a workable solution we would be very open to accepting a PR!

@onassar

This comment has been minimized.

Copy link

onassar commented Sep 24, 2018

@jylopez

This comment has been minimized.

Copy link

jylopez commented Dec 24, 2018

@onassar and future googlers, I just used this instead of util:

var url = `${(!cookie.httpOnly && cookie.secure) ? 'https' : 'http'}://${cookie.domain}${cookie.path}`
@onassar

This comment has been minimized.

Copy link

onassar commented Dec 24, 2018

Thanks @jylopez I'll give that a go when I get back around to things. Merry Christmas :)

@twolfson

This comment has been minimized.

Copy link
Contributor

twolfson commented Mar 28, 2019

Corrections to the posted solutions: The ternary should only be for cookie.secure, not also cookie.httpOnly -- otherwise we will attempt to set cookie.secure on using a http URL which will fail

var url = util.format('%s://%s%s', cookie.secure ? 'https' : 'http', cookie.domain, cookie.path);
var url = `${cookie.secure ? 'https' : 'http'}://${cookie.domain}${cookie.path}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.