-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathngx-cookies.ts
More file actions
110 lines (86 loc) · 2.8 KB
/
Copy pathngx-cookies.ts
File metadata and controls
110 lines (86 loc) · 2.8 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
export class NgXCookies {
/**
* Gets a cookie by it's name
*
* @param {string} cookieName Name/ID of cookie
* @returns Cookies value
*/
public static getCookie(cookieName: string): string {
let cookieExists = NgXCookies.exists(cookieName);
if (cookieExists)
{
cookieName = encodeURIComponent(cookieName);
let regexp = new RegExp('(?:^' + cookieName + '|;\\s*' + cookieName + ')=(.*?)(?:;|$)', 'g');
let cookies = regexp.exec(document.cookie);
return decodeURIComponent(cookies[1]);
}
else {
return '';
}
}
/**
* Sets the Cookie by name
*
* @param {string} cookieName Name/ID of cookie
* @param {string} value cookie value
* @param {number} validity expiration date of cookie (default is minutes).
* @param {string} validityType Unit for specifying validity time: days || hours . If left blank, default validity is in minutes
* @param {string} domain Set a specific domain for the cookie to be reachable at
* @param {string} path Path relative to domain
* @param {boolean} needsSecureConnection true/false if cookie can only be accessed through secure
*/
public static setCookie(cookieName: string, value: string, validity?: number, validityType?: string, domain?: string, path?: string, needsSecureConnection?: boolean) {
let cookieStr = encodeURIComponent(cookieName) + '=' + encodeURIComponent(value) + ';';
/**
* Sets validity of cookie
*/
if (validity) {
let fullValidity = validity * 1000 * 60;
if (validityType == 'days') {
fullValidity *= (60*24);
}
else if (validityType == 'hours') {
fullValidity *= 60;
}
let daysValid = new Date(new Date().getTime() + fullValidity);
cookieStr += 'expires=' + daysValid.toUTCString() + ';';
}
if (path) {
cookieStr += 'path=' + path + ';';
}
if (domain) {
cookieStr += 'domain=' + domain + ';';
}
if (needsSecureConnection) {
cookieStr += 'secure;';
}
document.cookie = cookieStr;
}
/**
* Deletes a specific cookie
*
* @param {string} cookieName Name/ID of cookie
* @param {string} domain Set a specific domain for the cookie to be reachable at
* @param {string} path Path relative to domain
*/
public static deleteCookie(cookieName: string, domain?: string, path?: string)
{
let cookieExists = NgXCookies.exists(cookieName);
// If the cookie exists
if (cookieExists)
{
NgXCookies.setCookie(cookieName, '', -1, '', domain, path);
}
}
/**
* Checks if the cookie exists
* @param {string} cookieName Name/ID of cookie
* @returns existence of the cookie
*/
public static exists(cookieName: string): boolean {
cookieName = encodeURIComponent(cookieName);
let regexp = new RegExp('(?:^' + cookieName + '|;\\s*' + cookieName + ')=(.*?)(?:;|$)', 'g');
let exists = regexp.test(document.cookie);
return exists;
}
}