Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Electron, is the default session persistent?

I'm using Electron (v1.2.7) and I need session cookies to persist between app restarts (for auth).

Is it possible? I know in Chrome when you set "Continue where you left off", the session is kept but not sure if this works the same in Electron.

As a workaround, I tried storing the session cookies also as non session cookies but it failed with a generic error.

Clarification: I'm not setting the session cookies they are set by other webpages during authentication.

like image 681
Amit Apple Avatar asked Aug 24 '16 17:08

Amit Apple


2 Answers

The default session is persistent, but if you set cookies using session.defaultSession.cookies.set() you must set the expiration date in order for the cookie to be persisted.

like image 101
Vadim Macagon Avatar answered Oct 03 '22 21:10

Vadim Macagon


You can persist cookies setting the session and a expirationDate

This example was made on Angularjs

var session = require('electron').remote.session;
var ses = session.fromPartition('persist:name');

this.put = function (data, name) {
        var expiration = new Date();
        var hour = expiration.getHours();
        hour = hour + 6;
        expiration.setHours(hour);
        ses.cookies.set({
            url: BaseURL,
            name: name,
            value: data,
            session: true,
            expirationDate: expiration.getTime()
        }, function (error) {
            /*console.log(error);*/
        });
    };

PD: A problem that i had was if i didn't persist them, after a fews reloads my cookies get lost. So in this example i'd persist them 6 hours

like image 43
Paulo Galdo Sandoval Avatar answered Oct 03 '22 21:10

Paulo Galdo Sandoval