3

How to add expiry time in hours for a cookie, I am able to do with days.

Cookie.set('SESSION_TOKEN', sessionToken, 6); // here 6 is days

I want in hours. I want cookie to be expired in 6 hours.

Cookie.set('SESSION_TOKEN', sessionToken, 6(hours)); // 6 hours

Below is the code I am using, but I am getting an error "Argument of type 'Date' is not assignable to parameter of type 'number'"

setCookie(userObject) {
    let expire = new Date();
    var time = Date.now() + ((3600 * 1000) * 6);
    expire.setTime(time);
    console.log("expire "+expire);
    Cookie.set('USER_NAME',Base64.encode(userObject.user.email),expire);
    Cookie.set('SESSION_TOKEN',userObject.sessionToken,expire);
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
praveen kumar
  • 1,474
  • 4
  • 13
  • 19
  • Are you sure you're using `angular2-cookie` and not `ng2-cookies`? Cause I couldn't find any `set` method in angula2-cookie API. The only way you can set a cookie in the current version is `put(key: string, value: string, options?: CookieOptionsArgs): void;` – Alexus Feb 02 '17 at 09:49
  • can you try setting the second parameter of the 'set' method to empty string (``Cookie.set('test','', expire)``) and tell me what output you get? Also do you get this error while transpiling your ts code to js? – Alexus Feb 03 '17 at 11:50

3 Answers3

5

I had done like this, it worked for me.

 Cookie.set('SESSION_TOKEN',userObject.sessionToken,0.25);

Actually if we give 1 it stays long for 24hrs, So I applied mathematical logic to it, i.e. for 6 hours.

1 day -> 24 hrs
? day -> 6 hrs
(1*6)/24 -> 0.25 i.e. 6hrs
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
praveen kumar
  • 1,474
  • 4
  • 13
  • 19
  • I ran into the type mismatch although the code in the library seems to handle dates just fine, and passing fractions is a good workaround. – Fernando Mazzon Feb 20 '17 at 17:48
0

As far as I know, the current version of ng2-cookies doesn't contain such a feature which would allow you to define hours instead of days. Therefore you have to manually calculate the time when the cookie will expire.

ng2-cookies/src/cookie.ts

/**
 * Save the Cookie
 *
 * @param  {string} name Cookie's identification
 * @param  {string} value Cookie's value
 * @param  {number} expires Cookie's expiration date in days from now or at a specific date from a Date object. If it's undefined the cookie is a session Cookie
 * @param  {string} path Path relative to the domain where the cookie should be avaiable. Default /
 * @param  {string} domain Domain where the cookie should be avaiable. Default current domain
 * @param  {boolean} secure If true, the cookie will only be available through a secured connection
 */
public static set(name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean) {

So I suppose something like this may work.

import { Cookie } from 'ng2-cookies';

var expire = new Date();
var time = Date.now() + ((3600 * 1000) * 6); // current time + 6 hours ///
expire.setTime(time);

Cookie.set("test", "", expire);
Alexus
  • 1,887
  • 1
  • 23
  • 50
  • I am getting this error "Argument of type 'Date' is not assignable to parameter of type 'number'" at Cookie.set('SESSION_TOKEN',sessionToken, expire); – praveen kumar Feb 02 '17 at 11:25
  • are you using the latest version (`ng2-cookies@1.0.4` ) ? Cuase I just tried it and it's working. (also updated my answer with a short example) – Alexus Feb 02 '17 at 12:29
  • Yes, I am doing the latest version of ng2-cookies i.e.("ng2-cookies": "^1.0.4"). No change, getting same error **Argument of type 'Date' is not assignable to parameter of type 'number'** – praveen kumar Feb 03 '17 at 06:31
  • can you please update your question above with a short example? I see no reason why it shouldn't work. – Alexus Feb 03 '17 at 09:40
  • I have added my code. Please check it once. And mention me the changes to be done. **note:** I am using angular2 – praveen kumar Feb 03 '17 at 10:41
  • I'd recommend checking the `ng2-cookies` package manually. Go to your the `node_modules/ng2-cookies/src/cookie.ts` and see how are the parameters defined for 'set' method. Or better do the `npm cache clean` and reinstall the packages. – Alexus Feb 03 '17 at 11:57
  • I have done, but no change in it. @Alexus if you see the params, there is no 'Date' variable. That is the reason why I am not getting. So I tried with different method, I just posted my answer. – praveen kumar Feb 13 '17 at 09:09
0

You could also do something like below and set it in hours for less complexity:

const now = new Date();
now.setHours(now.getHours() + 8);
Cookie.set('SESSION_TOKEN', sessionToken, now);

Then you can set your preferable expiration time in hours easily by changing the number in setHours :)

TheodoreTsg
  • 510
  • 3
  • 9
  • 23