Reputation: 580
I want to set the secure flag in my cookie when I create it. I think I have the solution but I want to be sure in order to continue. I use the ngx-cookie-service to set my cookie.
Here is my code:
const now = new Date();
now.setHours(now.getHours() + 8);
const secureFlag = true;
this.cookieService.set('usertype', 'agent', now, '/', '/', secureFlag);
The thing is that I don't know if I have to declare the 4th and 5th parameter like that because if I don't declare them it shows error.
For example I try this:
const now = new Date();
now.setHours(now.getHours() + 8);
const secureFlag = true;
this.cookieService.set('usertype', 'agent', now, secureFlag);
and it warns me with Argument of type 'true' is not assignable to parameter of type 'string'
Do I have to use '/'
for path
and domain
parameters when I don't want to define them?
Upvotes: 9
Views: 20097
Reputation: 151
You can try below one ("Lax"/"Strict"):-
const secureFlag = true; this.cookieService.set('cookieName', 'somevalue', undefined, undefined,'undefined, secureFlag , "Strict");
Upvotes: 1
Reputation: 71
This will work, also to do testing disable SameSite
by default cookies on Google Chrome if you are having problems with Google Chrome.
Paste this into your browser and it will take you to the SameSite settings and disable.
chrome://flags/#same-site-by-default-cookies
this.cookieService.set('sessionuser', username, 1 , '/', 'localhost', false, "Lax" );
Upvotes: 7
Reputation: 11
I think you alreadey solved in one of your comments, since is the solution I try and succeded, only add a last parameter (Lax), but is not necesary:
this.cookieService.set('usertype', 'agent', now, null, null, secureFlag, 'Lax');
I try to add '/' in the path and domain parameters, that only works in 'path', but setting both to null do the work.
Upvotes: 1
Reputation: 261
If you are in development & you don't have ssl, then secureFlag: true
, might not work.
e.g .
this.cookieService.set('cookieName', 'somevalue', 1, '/','localhost', false, "Strict");
This should work :)
Upvotes: 2
Reputation: 443
The get method of CookieService supports the following parameters:
/**
* @param name Cookie name
* @param value Cookie value
* @param expires Number of days until the cookies expires or an actual `Date`
* @param path Cookie path
* @param domain Cookie domain
* @param secure Secure flag
* @param sameSite OWASP samesite token `Lax`, `None`, or `Strict`. Defaults to `None`
*/
set(name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict'): void;
The error Argument of type 'true' is not assignable to parameter of type 'string'
it’s because you’re sending the secure parameter instead of path.
Upvotes: 4
Reputation: 51
Does this post addresses the same issue? I think the answer there could help you. Angular4x : ngx-cookie-service with expire parameter
Upvotes: 1