Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
ngx-cookie-service
Advanced tools
The ngx-cookie-service is an Angular service for handling cookies. It provides a simple API to set, get, and delete cookies in an Angular application.
Set a Cookie
This feature allows you to set a cookie with a specified name and value. In this example, a cookie named 'test' with the value 'Hello World' is set.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
this.cookieService.set('test', 'Hello World');
Get a Cookie
This feature allows you to retrieve the value of a cookie by its name. In this example, the value of the cookie named 'test' is retrieved and logged to the console.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
const value = this.cookieService.get('test');
console.log(value);
Delete a Cookie
This feature allows you to delete a cookie by its name. In this example, the cookie named 'test' is deleted.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
this.cookieService.delete('test');
Check if a Cookie Exists
This feature allows you to check if a cookie exists by its name. In this example, it checks if the cookie named 'test' exists and logs the result to the console.
import { CookieService } from 'ngx-cookie-service';
constructor(private cookieService: CookieService) { }
const exists = this.cookieService.check('test');
console.log(exists);
ngx-cookie is another Angular package for handling cookies. It provides similar functionalities to ngx-cookie-service, such as setting, getting, and deleting cookies. However, ngx-cookie also offers additional features like cookie serialization and deserialization.
angular2-cookie is an older package for managing cookies in Angular applications. It offers basic cookie operations similar to ngx-cookie-service but is less actively maintained and may not support the latest Angular versions.
Angular service to read, set and delete browser cookies. The experienced team behind Studytube will take care of our cookie service from now on.
Note:
ViewEngine
support has been removed on 13.x.x. See compatability matrix for details
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!
https://stackblitz.com/edit/angular-ivy-1lrgdt?file=src%2Fapp%2Fapp.component.ts
ViewEngine
support has been removed on 13.x.x. For Angular versions 13.x.x or later use the latest version of the
library. For versions <=12.x.x, use 12.0.3 version
Angular Version | Supported Version |
---|---|
13.x.x or later (Ivy) | 13.x.x or later |
<=12.x.x (View Engine) | 12.0.3 |
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.
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'.
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:
The following general steps are usually very helpful when debugging problems with this cookie service or cookies in general:
document.cookie
document.cookie
manually (i.e. in a console of your choice)?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.
We are working on it. If you are interested in helping us out, see PR
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:
FAQs
Angular cookie service
The npm package ngx-cookie-service receives a total of 326,152 weekly downloads. As such, ngx-cookie-service popularity was classified as popular.
We found that ngx-cookie-service demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.