Sessions and cookies

Sessions and cookies are important when your application is connected to a remote host. In a web page, by default, you can get the cookie associated with the domain that your web page is loaded from using cookie object. In Electron, the cookies class provides the access to the cookie object for each domain. An instance of this cookies class can be accessed by using cookies property of the Session class. For example, all the cookies stored in the Electron shell can be queried as follows from your main process:

const { session, app, BrowserWindow } = require('electron');

let appShell;
let appUrl = 'file://' + __dirname + '/index.html';

function createElectronShell() {
appShell = new BrowserWindow({ width: 800, height: 600 });
appShell.loadURL(appUrl);

getAllCookies();
}

function getAllCookies() {
session.defaultSession.cookies.get({}, (error, cookies) => {
// Cookies can be accessed here using cookies variable
});
}

app.on('ready', createElectronShell);

In this example, the cookies.get method will retrieve the cookies stored in the Electron shell. However, we did not pass any cookie name or URL,  so that the Electron will list out all the cookies available inside the shell. You can pass the domain and cookie name to get the filtered cookies specific to the passed domain or cookie name. The following example queries all the cookies stored against a specific domain or URL:

const { session,BrowserWindow, app } = require('electron'); app.on('ready', () => { 
let domainFilter = {
url: 'http://www.google.com'
};
session.defaultSession.cookies.get(domainFilter, (error, cookies) => {
if(error)
throw new Error('Could not read cookie');

console.log(cookies);
});
});

This code gives you all the cookies that are set against the google.com URL. If you pass a name with a URL, you can get the specific cookie assigned to that URL with the name passed:

let domainFilter = { 
url: 'http://www.google.com',
name: 'auth_user'
};

This will give the cookie stored with name auth_user against the google.com URL. The same signature can be used to set cookie back to the shell.  This will overwrite the cookie if the name already exists:

const cookie = {
url: 'http://www.google.com',
name: 'auth_name',
value: 'username'
};
session.defaultSession.cookies.set(cookie, (error) => {});

The cookie class provides changed event that can be used to watch the value changes in a cookie. Electron emits this event whenever a cookie is added, deleted, or a value is changed:

session.defaultSession.cookies.addEventListener('changed', (
event, cookie, cause, removed
) => {
if(removed) console.log('Cookie ' + cookie.name + ' is removed');
else console.log('Changed action : ' + cause);
});

The removed flag will be true when you remove a cookie from the shell. You can get the type of action you made on a cookie through the cause variable. This returns a string and can be one of the following values:

  • explicit 
  • overwrite 
  • expired 
  • evicted 
  • expired-overwrite 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.37.147