ngx-cookie-service 11.0.1 → 11.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +160 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,24 +1,171 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NGX Cookie Service
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angular 9 IVY Ready service for cookies. Originally based on the [ng2-cookies](https://www.npmjs.com/package/ng2-cookies) library.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
For versions <9, please use 2.3.0 version of library.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
> Note: Don't forget to add `--project ngx-cookie-service` or else it will be added to the default project in your `angular.json` file.
|
|
7
|
+
The experienced team behind [Studytube](https://www.studytube.nl/) will take care of our cookie service from now on.
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
# Installation
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
```bash
|
|
12
|
+
npm install ngx-cookie-service --save
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
# or
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
yarn add ngx-cookie-service
|
|
17
|
+
```
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
Add the cookie service to your `app.module.ts` as a provider:
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
```typescript
|
|
22
|
+
import { CookieService } from 'ngx-cookie-service';
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
@NgModule({
|
|
25
|
+
...
|
|
26
|
+
providers: [ CookieService ],
|
|
27
|
+
...
|
|
28
|
+
})
|
|
29
|
+
export class AppModule { }
|
|
30
|
+
```
|
|
23
31
|
|
|
24
|
-
|
|
32
|
+
Then, import and inject it into a constructor:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
constructor( private cookieService: CookieService ) {
|
|
36
|
+
this.cookieService.set( 'Test', 'Hello World' );
|
|
37
|
+
this.cookieValue = this.cookieService.get('Test');
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
That's it!
|
|
42
|
+
|
|
43
|
+
# Methods
|
|
44
|
+
|
|
45
|
+
## check( name: string ): boolean;
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const cookieExists: boolean = cookieService.check('test');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Checks if a cookie with the given`name` can be accessed or found.
|
|
52
|
+
|
|
53
|
+
## get( name: string ): string;
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
const value: string = cookieService.get('test');
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Gets the value of the cookie with the specified `name`.
|
|
60
|
+
|
|
61
|
+
## getAll(): {};
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const allCookies: {} = cookieService.getAll();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Returns a map of key-value pairs for cookies that can be accessed.
|
|
68
|
+
|
|
69
|
+
## set( name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'Strict' | 'None' ): void;
|
|
70
|
+
## set( name: string, value: string, options?: { expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict'}): void;
|
|
71
|
+
```typescript
|
|
72
|
+
cookieService.set( 'test', 'Hello World' );
|
|
73
|
+
cookieService.set( 'test', 'Hello World', {expires: 2, sameSite: 'Lax'});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Sets a cookie with the specified `name` and `value`. It is good practice to specify a path. If you are unsure about the path value, use `'/'`. If no path or domain is explicitly defined, the current location is assumed. `sameSite` defaults to `Lax`.
|
|
77
|
+
|
|
78
|
+
**Important:** For security reasons, it is not possible to define cookies for other domains. Browsers do not allow this. Read [this](https://stackoverflow.com/a/1063760) and [this](https://stackoverflow.com/a/17777005/1007003) StackOverflow answer for a more in-depth explanation.
|
|
79
|
+
|
|
80
|
+
**Important:** Browsers do not accept cookies flagged sameSite = 'None' if secure flag isn't set as well. CookieService will override the secure flag to true if sameSite='None'.
|
|
81
|
+
|
|
82
|
+
## delete( name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax'): void;
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
cookieService.delete('test');
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Deletes a cookie with the specified `name`. It is best practice to always define a path. If you are unsure about the path value, use `'/'`.
|
|
89
|
+
|
|
90
|
+
**Important:** For security reasons, it is not possible to delete cookies for other domains. Browsers do not allow this. Read [this](https://stackoverflow.com/a/1063760) and [this](https://stackoverflow.com/a/17777005/1007003) StackOverflow answer for a more in-depth explanation.
|
|
91
|
+
|
|
92
|
+
## deleteAll( path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
cookieService.deleteAll();
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Deletes all cookies that can currently be accessed. It is best practice to always define a path. If you are unsure about the path value, use `'/'`.
|
|
99
|
+
|
|
100
|
+
# FAQ
|
|
101
|
+
|
|
102
|
+
## General tips
|
|
103
|
+
|
|
104
|
+
Checking out the following resources usually solves most of the problems people seem to have with this cookie service:
|
|
105
|
+
|
|
106
|
+
* [article about cookies in general @MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) (recommended read!)
|
|
107
|
+
* [common localhost problems @StackOverflow](https://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain)
|
|
108
|
+
* [problems with secure cookies @StackOverflow](https://stackoverflow.com/questions/8064318/how-to-read-a-secure-cookie-using-javascript)
|
|
109
|
+
* [how do browser cookie domains work? @StackOverflow](https://stackoverflow.com/questions/1062963/how-do-browser-cookie-domains-work)
|
|
110
|
+
* [get cookies from different paths](https://github.com/7leads/ngx-cookie-service/issues/7#issuecomment-351321518)
|
|
111
|
+
|
|
112
|
+
The following general steps are usually very helpful when debugging problems with this cookie service or cookies in general:
|
|
113
|
+
|
|
114
|
+
* check out if there are any [open](https://github.com/stevermeister/ngx-cookie-service/issues) or [closed](https://github.com/stevermeister/ngx-cookie-service/issues?q=is%3Aissue+is%3Aclosed) issues that answer your question
|
|
115
|
+
* check out the actual value(s) of `document.cookie`
|
|
116
|
+
* does it work if you use `document.cookie` manually (i.e. in a console of your choice)?
|
|
117
|
+
* set explicit paths for your cookies
|
|
118
|
+
* [explain to your local rubber duck why your code should work and why it (probably) does not](https://en.wikipedia.org/wiki/Rubber_duck_debugging)
|
|
119
|
+
|
|
120
|
+
# I am always getting a "token missing" or "no provider" error.
|
|
121
|
+
|
|
122
|
+
Package managers are a well known source of frustration. If you have "token missing" or "no provider" errors, a simple re-installation of your node modules might suffice:
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
rm -rf node_modules
|
|
126
|
+
yarn # or `npm install`
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## I have a problem with framework X or library Y. What can I do?
|
|
130
|
+
|
|
131
|
+
Please be aware that we cannot help you with problems that are out of scope. For example, we cannot debug a Symfony or Springboot application for you. In that case, you are better off asking the nice folks over at [StackOverflow](https://stackoverflow.com/) for help.
|
|
132
|
+
|
|
133
|
+
## Do you support Angular Universal?
|
|
134
|
+
|
|
135
|
+
There is an [issue](https://github.com/7leads/ngx-cookie-service/issues/1) for that. Check out [this comment](https://github.com/7leads/ngx-cookie-service/issues/1#issuecomment-361150174) for more information about future support.
|
|
136
|
+
|
|
137
|
+
# Opening issues
|
|
138
|
+
|
|
139
|
+
Please make sure to check out our FAQ before you open a new issue. Also, try to give us as much information as you can when you open an issue. Maybe you can even supply a test environment or test cases, if necessary?
|
|
140
|
+
|
|
141
|
+
# Contributing
|
|
142
|
+
|
|
143
|
+
We are happy to accept pull requests or test cases for things that do not work. Feel free to submit one of those.
|
|
144
|
+
|
|
145
|
+
However, we will only accept pull requests that pass all tests and include some new ones (as long as it makes sense to add them, of course).
|
|
146
|
+
|
|
147
|
+
* [Open a new pull request](https://github.com/stevermeister/ngx-cookie-service/compare)
|
|
148
|
+
|
|
149
|
+
# Author
|
|
150
|
+
|
|
151
|
+
This cookie service is brought to you by [7leads GmbH](http://www.7leads.org/). We built it for one of our apps, because the other cookie packages we found were either not designed "the Angular way" or caused trouble during AOT compilation.
|
|
152
|
+
|
|
153
|
+
# Contributors
|
|
154
|
+
|
|
155
|
+
Thanks to all contributors:
|
|
156
|
+
|
|
157
|
+
* [paroe](https://github.com/paroe)
|
|
158
|
+
* [CunningFatalist](https://github.com/CunningFatalist)
|
|
159
|
+
* [kthy](https://github.com/kthy)
|
|
160
|
+
* [JaredClemence](https://github.com/JaredClemence)
|
|
161
|
+
* [flakolefluk](https://github.com/flakolefluk)
|
|
162
|
+
* [mattbanks](https://github.com/mattbanks)
|
|
163
|
+
* [DBaker85](https://github.com/DBaker85)
|
|
164
|
+
* [mattlewis92](https://github.com/mattlewis92)
|
|
165
|
+
* [IceBreakerG](https://github.com/IceBreakerG)
|
|
166
|
+
* [rojedalopez](https://github.com/rojedalopez)
|
|
167
|
+
* [Nikel163](https://github.com/Nikel163)
|
|
168
|
+
|
|
169
|
+
# License
|
|
170
|
+
|
|
171
|
+
[MIT](https://github.com/stevermeister/ngx-cookie-service/blob/master/LICENSE)
|