17.1.0 • Published 12 days ago

ngx-cookie-service v17.1.0

Weekly downloads
196,792
License
MIT
Repository
github
Last release
12 days ago

NGX Cookie Service

build

Angular 12 IVY Ready service for cookies. Originally based on the ng2-cookies library. For versions < 9.x.x, please use 2.3.0 version of library.

The experienced team behind Studytube will take care of our cookie service from now on.

Installation

npm install ngx-cookie-service --save

# or

yarn add ngx-cookie-service

Add the cookie service to your app.module.ts as a provider:

import { CookieService } from 'ngx-cookie-service';

@NgModule({
  ...
    providers:
[CookieService],
...
})

export class AppModule {
}

Then, import and inject it into a constructor:

constructor(private
cookieService: CookieService
)
{
  this.cookieService.set('Test', 'Hello World');
  this.cookieValue = this.cookieService.get('Test');
}

That's it!

Methods

check( name: string ): boolean;

const cookieExists: boolean = cookieService.check('test');

Checks if a cookie with the givenname can be accessed or found.

get( name: string ): string;

const value: string = cookieService.get('test');

Gets the value of the cookie with the specified name.

getAll(): {};

const allCookies: {} = cookieService.getAll();

Returns a map of key-value pairs for cookies that can be accessed.

set( name: string, value: string, expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'Strict' | 'None' ): void;

set( name: string, value: string, options?: { expires?: number | Date, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict'}): void;

cookieService.set('test', 'Hello World');
cookieService.set('test', 'Hello World', { expires: 2, sameSite: 'Lax' });

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.

Important: For security reasons, it is not possible to define cookies for other domains. Browsers do not allow this. Read this and this StackOverflow answer for a more in-depth explanation.

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'.

delete( name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax'): void;

cookieService.delete('test');

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 '/'.

Important: For security reasons, it is not possible to delete cookies for other domains. Browsers do not allow this. Read this and this StackOverflow answer for a more in-depth explanation.

deleteAll( path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;

cookieService.deleteAll();

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 '/'.

FAQ

General tips

Checking out the following resources usually solves most of the problems people seem to have with this cookie service:

The following general steps are usually very helpful when debugging problems with this cookie service or cookies in general:

I am always getting a "token missing" or "no provider" error.

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:

rm -rf node_modules
yarn # or `npm install`

I have a problem with framework X or library Y. What can I do?

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 for help.

Do you support Angular Universal?

There is an issue for that. Check out this comment for more information about future support.

Opening issues

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?

Contributing

We are happy to accept pull requests or test cases for things that do not work. Feel free to submit one of those.

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).

Author

This cookie service is brought to you by 7leads GmbH. 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.

Contributors

Thanks to all contributors:

License

MIT

smart-accounts-experienceselection-ngsedapta-core-devng-drivecommonsthermoconverge@zdavy4k/headermona-webng-wc-frontendgeneric-dashbord@mirisbowring/primboard-webv6-angularv6-testkc-layoutsportcrmrxmx-coretestalkhawarizmialkhawarizmitmfe-commonform-template-library@tounes_lina/elbaladiya-core@infinitebrahmanuniverse/nolb-ngx-c@everything-registry/sub-chunk-2282@ebolanos/libreria-login@edgarguzman/libreria-login@empowerlabs/components@jenna.meiring/ng-edge-chat@jasoftware/service-django-jwt@federico1818/passport@fluzia/testfluzia@genniuxapps/components@lxmetestorg/login-test@ma_tkuchne/front-abstract@ma_tkuchne/sc5-utilities@q2devops/q2-bientainova-core@mprisma/components@esfaenza/core@ericklestrade/libreria-loginlegalconnect.portals.membership@terragon/terragonliqingqianlib-chat-box-dpslib-chat-box-test@testldc/shared-kernel@seniorsistemas/platform-componentsiqs-libs-clientshell2-angularkakadustudio-angular-cli-library@rucken/corekn-chat-appitxadummy-pengajuan-dummyitxadummy-template-library@gsp-svc/rule-helpjsc-applxme-login-testma-shop-basemfecommonmetis-ui-consent-managementngx-unificatorngx-weaverngx-classificationngx-core-libraryngx-mango-corengx-doangular-core-modulenode-angular-app-templatengpl-rx-cookie-storenoctua-form-baseangular-jwt-codaangular-kickoff-templateangular-4-crud-modulepack-dashbordcms-feacc-erp-servicescockpit-framework-modulecontract-management-spaconsultant-account-webbizdoc.corepetrinet-jsclientstorechankyaclinic-carecrmspavmdkmksvms-shared-librarymicrofrontendworkspacedevicewisedevicewise-angularreviews-componentsxc-web-shareddashbord-sf-genericvx-teste-mark2vx-teste-mark3data-portal2myais-libmydemoappservicemenulistappmagenta-pengajuan-dummy@vonbraunlabs.com/angular-utils@vonbraunlabs/authb2b-frontend
18.0.0-next.1

12 days ago

17.1.0

2 months ago

17.1.0-rc.1

3 months ago

17.0.1-rc.1

4 months ago

17.0.1

4 months ago

17.0.0-rc.1

6 months ago

17.0.0-rc.0

6 months ago

17.0.0-rc.3

5 months ago

17.0.0-rc.2

6 months ago

17.0.0-next.2

7 months ago

16.1.0

5 months ago

17.0.0

5 months ago

16.0.2

6 months ago

17.0.0-next.1

7 months ago

17.0.0-next.0

8 months ago

16.0.1

8 months ago

16.0.0-rc.0

1 year ago

16.0.0

12 months ago

15.0.0-rc.2

1 year ago

15.0.0-rc.3

1 year ago

15.0.0-rc.1

1 year ago

15.0.0

1 year ago

14.0.0-rc.2

2 years ago

14.0.0-rc.1

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

14.0.0-rc.0

2 years ago

13.2.1

2 years ago

13.2.0

2 years ago

13.2.0-rc.1

2 years ago

13.1.1

2 years ago

13.1.2

2 years ago

13.1.0

2 years ago

13.1.2-rc.1

2 years ago

13.1.0-rc.1

2 years ago

13.0.1

2 years ago

13.0.0

2 years ago

12.1.0-rc.3

2 years ago

12.1.0-rc.2

2 years ago

12.1.0-rc.5

2 years ago

12.1.0-rc.4

2 years ago

12.1.0-rc.1

2 years ago

12.1.0-rc.0

2 years ago

12.0.3

3 years ago

12.0.1

3 years ago

12.0.2

3 years ago

12.0.0

3 years ago

11.0.2

3 years ago

11.0.0

3 years ago

11.0.1

3 years ago

10.1.1

4 years ago

10.0.1

4 years ago

10.0.0

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.10

6 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago