anupkawtia
anupkawtia

Reputation: 31

Unable to set secure flag for cookies using $cookies.putObject in Angularjs version 1.4.4

We are unable to set the secure flag for cookies using the $cookies service in Angularjs 1.4.4. We are using the putObject() method mentioned in https://code.angularjs.org/1.4.4/docs/api/ngCookies/service/$cookies for the same.

Below is the same code snippet we are using for the same:

var config = {
    secure: true
};
$cookies.putObject('foo', 'bar', config);

We cannot see the secure flag being set when we inspect the cookie that's being created. Also, if we try to do a $cookies.getObject('foo') we get an undefined object in response. We already have all the necessary modules like "ngCookies" injected in our application.

Upvotes: 3

Views: 5107

Answers (1)

Tracy B.
Tracy B.

Reputation: 63

I know this is quite late but maybe others will find it helpful.

In angular 1.4.x and 1.5.x I set cookies like

let config = {
    secure: true
};
$cookies.put('foo', 'bar', config);
$cookies.get('foo');

To test the cookies you actually have to be on a secure connection - you will not be able to set or get secure cookies on localhost (unless you want to put some work into creating a secure localhost).

Upvotes: 1

Related Questions