Unlike many other cookie management libraries, NG Cookie Manager offers extended functionalities that go beyond basic cookie creation and deletion. These include encryption support, prefix-based deletion, size calculation, and total cookie size management. This makes it ideal for developers who need more advanced cookie management options in their Angular applications.
- Set Cookies: Create new cookies with various options (expiration, path, domain, security).
- Get Cookies: Retrieve the value of cookies by name, with optional decryption.
- Delete Cookies: Remove cookies safely.
- Delete All Cookies: Remove all cookies with one method.
- Check Cookie Existence: Verify if a cookie exists.
- Delete Cookies by Prefix: Remove cookies whose names start with a specified prefix.
- Cookie Size Calculation: Determine the size of individual cookies or the total size of all cookies.
-
Secure Handling: Options for securing cookies with
SameSite
,Secure
, and encryption options. - Type Safety: Full TypeScript support for a better development experience.
Install the package via npm:
npm install ng-cookie-manager
Import the CookieManagerService in your Angular module or component:
import { CookieManagerService } from 'ng-cookie-manager';
Here’s an example of how to use the CookieManagerService in a component:
import { Component } from '@angular/core';
import { NgCookieManagerService } from 'ng-cookie-manager';
@Component({
selector: 'app-demo',
templateUrl: './demo.component.html',
})
export class DemoComponent {
constructor(private cookieManager: NgCookieManagerService) {}
// Example usage of setting a cookie
setCookie() {
this.cookieManager.setCookie('username', 'ndiayeD.', 7);
}
// Example usage of getting a cookie
getCookie() {
const userName = this.cookieManager.getCookie('username');
console.log('Username:', userName);
}
// Example usage of deleting a cookie
deleteCookie() {
this.cookieManager.deleteCookie('username');
}
// Check if a cookie exists
checkCookieExists() {
const exists = this.cookieManager.hasCookie('username');
console.log('Cookie exists:', exists);
}
// Delete all cookies
deleteAllCookies() {
this.cookieManager.deleteAllCookies();
}
// Delete cookies by prefix
deleteCookiesByPrefix() {
this.cookieManager.deleteCookiesByPrefix('user');
}
// Get total size of all cookies
getTotalCookieSize() {
const totalSize = this.cookieManager.getTotalCookieSize();
console.log('Total size of all cookies:', totalSize, 'bytes');
}
}
Sets a new cookie with optional parameters for expiration, path, domain, and security.
setCookie(
name: string,
value: string,
expireDaysOrDate?: number | Date,
path?: string,
domain?: string,
secure?: boolean,
sameSite?: string,
encrypted?: boolean
): void;
- name: string - The name of the cookie.
- value: string - The value to be stored in the cookie.
- expireDaysOrDate: number | Date - Optional. The number of days until the cookie expires or a specific expiration date.
- path: string - Optional. The path within the site for which the cookie is valid.
- domain: string - Optional. The domain for which the cookie is valid.
- secure: boolean - Optional. If true, the cookie will only be transmitted over secure protocols (HTTPS).
- sameSite: string - Optional. Specifies the SameSite attribute for the cookie (Strict, Lax, or None).
- encrypted: boolean - Optional. If true, the cookie value will be encrypted before storage.
Retrieves the value of a cookie by its name, with support for decryption.
getCookie(name: string, encrypted?: boolean): string | null;
- name: string - The name of the cookie to retrieve.
- encrypted: boolean - Optional. If true, attempts to decrypt the cookie value.
- string | null - The value of the cookie if found, or null if the cookie does not exist.
Removes a cookie by its name.
deleteCookie(name: string, path?: string, domain?: string): void;
- name: string - The name of the cookie to delete.
- path: string - Optional. The path within the site for which the cookie is valid.
- domain: string - Optional. The domain for which the cookie is valid.
Checks if a cookie with the specified name exists.
hasCookie(name: string): boolean;
- name: The name of the cookie to check.
- true if the cookie exists, otherwise false.
Deletes all cookies.
deleteAllCookies(path?: string, domain?: string): void;
- path: Optional. The path within the site for which the cookie is valid.
- domain: Optional. The domain for which the cookies are valid.
Deletes cookies whose names start with a specific prefix.
deleteCookiesByPrefix(prefix: string, path?: string, domain?: string): void;
- prefix: The prefix to match cookies by their name.
- path: Optional. The path within the site for which the cookies are valid.
- domain: Optional. The domain for which the cookies are valid.
Gets the size of a specific cookie.
getCookieSize(name: string): number;
- name: The name of the cookie to get the size of.
- The size of the cookie in bytes.
Calculates the total size of all cookies.
getTotalCookieSize(): number;
- The total size of all cookies in bytes.
Feel free to contribute to the project by submitting pull requests or issues. All contributions are welcome!
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request with a clear description of your changes.
This project is licensed under the MIT License - see the LICENSE
file for details.