-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCookieOptions.ts
30 lines (28 loc) · 984 Bytes
/
CookieOptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export enum SameSiteValue {
strict = "Strict",
lax = "Lax",
none = "None"
}
export class CookieOptions {
expires?: Date;
maxAge?: Number;
domain?: string;
path?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: SameSiteValue;
constructor(obj: object) {
obj && Object.assign(this, obj);
}
public toString(): string {
const opts: string[] = [];
if (this.expires) opts.push(`Expires=${this.expires.toUTCString()}`);
if (this.maxAge) opts.push(`Max-Age=${this.maxAge}`);
if (this.domain) opts.push(`Domain=${this.domain}`);
opts.push(this.path ? `Path=${this.path}` : "Path=/"); // we default to the whole domain as the standard default is confusing
if (this.secure) opts.push("Secure");
if (this.httpOnly) opts.push("HttpOnly");
if (this.sameSite) opts.push(`SameSite=${this.sameSite}`);
return opts.length ? '; ' + opts.join('; ') : '';
}
}