Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set expiration date for cookie in AngularJS

  1. We want to store User's Authorization information in cookie which should not be lost upon refresh (F5) of browser.

  2. We want to store authorization info in "permanent-cookie" in case user has opted for "Remember Me" check box at the time of log-on.

like image 323
Anand Avatar asked Sep 27 '12 14:09

Anand


People also ask

How do you set the expiration date on cookies?

Set an expiration date for a cookie This can be done easily by adding expires=expirationDate in UTC separated by semicolon from the name=value, as seen in the following example: document. cookie = "username=Max Brown; expires=Wed, 05 Aug 2020 23:00:00 UTC"; document.

How do I set cookies to expire at end of session?

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.

Which property is used to set expire date for cookie?

The Kind property of Expires is used to determine if the cookie is set to DateTimeKind.

What is the default expiration date of cookie?

What is the timeout of Cookie? The default time for a Cookie to expire is 30 minutes. The default Expires value for a cookie is not a static time, but it creates a Session cookie. This will stay active until the user closes their browser/clears their cookies.


1 Answers

This is possible in the 1.4.0 build of angular using the ngCookies module:

https://docs.angularjs.org/api/ngCookies/service/$cookies

angular.module('cookiesExample', ['ngCookies']) .controller('ExampleController', ['$cookies', function($cookies) {   // Find tomorrow's date.   var expireDate = new Date();   expireDate.setDate(expireDate.getDate() + 1);   // Setting a cookie   $cookies.put('myFavorite', 'oatmeal', {'expires': expireDate}); }]); 
like image 109
Geert van Dort Avatar answered Oct 19 '22 06:10

Geert van Dort