Vikas Bansal Vikas Bansal - 4 months ago 19
AngularJS Question

ElectronJs with AnuglarJs: Cookies are not holding values if application is restarted

I am learning ElectronJs so I have started with making a simple AngularJs application with it and here I started stumbling.

I want to store some persistent data like user token and email id and so on. So, in order to do this, I am using

$cookieStore
.

The given below example is working fine in (firefox, chrome) browser but when I am running the same example in electron browser window then it's storing the values temporarily and losing the values at the time of restart.

Please help, many thanks.

Example:

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-cookies.min.js"></script>
<meta charset="utf-8">
<title>AngularJS Cookies Example</title>
</head>

<body ng-controller="MyController">
{{platformCookie}} {{myFruit}} {{myFlower}}

<input type="button" value="setCookie" ng-click="setCookie()" />
<input type="button" value="getCookie" ng-click="getCookie()" />

<script>
var myApp = angular.module('myApp', ['ngCookies']);

myApp.controller('MyController', ['$scope', '$cookies', '$cookieStore',
function($scope, $cookies, $cookieStore) {
$cookies.userName = 'Vikas';

$scope.setCookie = function()
{
$scope.platformCookie = $cookies.userName;
$cookieStore.put('fruit', 'Apple');
$cookieStore.put('flower', 'Rose');



console.log('set cookies');
}

$scope.getCookie = function()
{
$scope.myFruit = $cookieStore.get('fruit');
$scope.myFlower = $cookieStore.get('flower');

console.log($scope.myFruit);
}

}]);
</script>
</body>

</html>


EDIT
Electron Session

I have also tried to use Electron Session in order to create persistent cookies, however, its also not working and getting disposed on the application restart.

code:

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

// Query all cookies associated with a specific URL.
ses.cookies.get({ url: 'http://www.github.com' }, (error, cookies) => {
console.log(cookies);
});

// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' };
ses.cookies.set(cookie, (error) => {
if (error)
console.error(error);
});


using this code when you run the application second time it should display the cookies data that had been set but no luck.

Answer

Cookies are not persistent if you do not give them expirationDate

let cookie = {
            url: 'http://www.drutas.com',
            name: 'userDetailsGoogle',
            value: JSON.stringify(userDetailsGoogle),
            expirationDate: 2093792393999
        };

If you give them expirationDate then cookies will persist