Reputation: 10864
I am using oauth (Stack Overflow) in an electron desktop app and there is a webview which loads the oauth url. I have a signout button in my app which will signout the user from Stack Overflow website and also from the app. How can I do this ?
How to remove all session cookies from the webview in electron app ?
Upvotes: 0
Views: 4198
Reputation: 970
If you want all cookies cleared, this would be the most straightforward way.
const { session } = require('electron');
session.defaultSession.clearStorageData({storages: ['cookies']})
.then(() => {
console.log('All cookies cleared');
})
.catch((error) => {
console.error('Failed to clear cookies: ', error);
});
It supports more complex requests. You can check the documentation here.
Upvotes: 1
Reputation: 1569
You can remove cookies using Electron's cookies.remove()
function (https://electron.atom.io/docs/api/cookies/#cookiesremoveurl-name-callback)
The trick is to convert cookie.domain
into url
.
import { session } from 'electron';
export default function deleteAllCookies() {
session.defaultSession.cookies.get({}, (error, cookies) => {
cookies.forEach((cookie) => {
let url = '';
// get prefix, like https://www.
url += cookie.secure ? 'https://' : 'http://';
url += cookie.domain.charAt(0) === '.' ? 'www' : '';
// append domain and path
url += cookie.domain;
url += cookie.path;
session.defaultSession.cookies.remove(url, cookie.name, (error) => {
if (error) console.log(`error removing cookie ${cookie.name}`, error);
});
});
});
}
Upvotes: 2