An (AOT ready) Angular (4.2+) service for cookies. Originally based on the ng2-cookies library.
We stopped working with Angular some time ago. At first we did not mind maintaining this package as a small side project, but we would finally like to move on to other projects.
However, we feel that this package is still useful. Therefore, we would really like to hand it over to a new maintainer instead of just deprecating it. If you would like to take care of ngx-cookie-service
in the future, feel free to shoot us an email: [email protected]
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 { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { CookieService } from 'ngx-cookie-service';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule, FormsModule, HttpModule ],
providers: [ CookieService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Then, import and inject it into a component:
import { Component, OnInit } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
@Component({
selector: 'demo-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
})
export class AppComponent implements OnInit {
cookieValue = 'UNKNOWN';
constructor( private cookieService: CookieService ) { }
ngOnInit(): void {
this.cookieService.set( 'Test', 'Hello World' );
this.cookieValue = this.cookieService.get('Test');
}
}
That's it!
- Run
npm run test
to run the tests for the cookie service (located in thedemo-app
folder) - Have a look at and play around with the
demo-app
to get to know the cookie service withnpm run start
(openhttp://localhost:4200/
in your favourite browser) - If you do not want to install this via NPM, you can run
npm run compile
and use the*.d.ts
and*.js
files in thedist-lib
folder
const cookieExists: boolean = cookieService.check('test');
Checks if a cookie with the givenname
can be accessed or found.
const value: string = cookieService.get('test');
Gets the value of the cookie with the specified name
.
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' ): void;
cookieService.set( 'test', 'Hello World' );
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.
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.
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.
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 '/'
.
Checking out the following resources usually solves most of the problems people seem to have with this cookie service:
- article about cookies in general @MDN (recommended read!)
- common localhost problems @StackOverflow
- problems with secure cookies @StackOverflow
- how do browser cookie domains work? @StackOverflow
- get cookies from different paths
The following general steps are usually very helpful when debugging problems with this cookie service or cookies in general:
- check out if there are any open or closed issues that answer your question
- check out the actual value(s) of
document.cookie
- does it work if you use
document.cookie
manually (i.e. in a console of your choice)? - set explicit paths for your cookies
- explain to your local rubber duck why your code should work and why it (probably) does not
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`
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.
There is an issue for that. Check out this comment for more information about future support.
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?
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).
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.
Thanks to all contributors: