1

I am using angular 1.2.32 in an old project and I need to know how can I set the expiry of cookie. I am using ngCookies for it and trying to set cookie using below line:

 $cookies.putObject("newotpcookieobject", '7668787', { 'expires': expiresValue });

here the expiresValue is datetime object in javascript, code for getting it is below:

var today = new Date();
            var expiresValue = new Date(today);
expiresValue.setMinutes(today.getMinutes() + 120);

I am new to angularJS framework.

Maher
  • 2,517
  • 1
  • 19
  • 32
  • Which version of ngCookies are you using ? If it's below 1.4, you can't set the expiration date of a cookie – Julien Jun 22 '21 at 07:29
  • Thanks @Julein, yes it is 1.2.32 and I am afraid that if expiration can't be set for this then I'm doomed!! – Sushant Rawat Jun 22 '21 at 09:15
  • Can I use angular-cookies version 1.4 and above with my angular 1.2.32 project?I mean I can refer the local file? would this work ? – Sushant Rawat Jun 22 '21 at 09:18
  • ngCookies and AngularJS should have the same version. It *could* work if you use ngCookies 1.4 and AngularJS 1.2 but there isn't any guarantees. You could either try to bump your AngularJS version or you could default to the native cookies API – Julien Jun 22 '21 at 09:26
  • Well I am using asp.net web api (C#), I tried to write cookie into client side from server (initially) but even though the cookie was created at server side somehow it was not writing it to the client side, if you could suggest something on this then thanks already! – Sushant Rawat Jun 22 '21 at 09:33
  • 1
    You can check this question https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie – Julien Jun 22 '21 at 09:34
  • 1
    Thanks for the help, your last comment worked! – Sushant Rawat Jun 23 '21 at 05:41

1 Answers1

1

As discussed in the comments, your issue lies in the version of ngCookies. The possibility to set the expiration date on a cookie is only allowed on 1.4 or greater.

Since your AngularJS version is 1.2.32, you should bump the version of ngCookies and AngularJS to, at least, 1.4.0.

If you can't change the versions, you could default to the native cookies API. More informations on the native cookies API can be found in this question

Julien
  • 2,256
  • 3
  • 26
  • 31