1.3.2 • Published 5 years ago

ngx-cookie-monsters v1.3.2

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

ngx-cookie-monster

Table of contents:

Getting Started

Installation

You can install this package locally with npm. To get the latest stable version and update package.json file:

npm install ngx-cookie-monsters --save

Usage

CookieModule should be registered in the AppModule with forRoot() static method and with forChild() in the child modules.\ These methods accepts CookieOptions objects as well. Leave it blank for the defaults.

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { CookieModule } from 'ngx-cookie-monster';

import { AppComponent }  from './app.component';

@NgModule({
  imports: [ BrowserModule, CookieModule.forRoot() ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }
import { Component } from '@angular/core';
import { NgxCookieMonsterService } from 'ngx-cookie-monster';

@Component({
    selector: 'nom-nom-nom',
    template: '<h1>come to the dark side, we have cookies</h1>'
})

export class AppComponent { 
  constructor(private cookieService: NgxCookieMonsterService){}
  
  getCookie(key: string) {
    return this.cookieService.get(key);
  }
}

CookieService

get()

Returns the value of given cookie key.

/**
 * @param key ID for lookup.
 * @returns Raw cookie value as string.
 */
get(key: string): string;

getObject()

Returns a deserialized Object of given cookie.

/**
 * @param key Id to use for lookup.
 * @returns deserialized cookied value.
 */
getObject(key: string): Object;

getAll()

Returns a key value object with all cookies

/**
 * @returns All cookies
 */
getAll(): any;

exists()

Evaluates if cookie exists

/**
 * @param key ID for lookup
 * @returns cookie existance
 */
exists(key: string): boolean;

create()

Create a cookie for the given key

/**
 * @param key ID
 * @param value stored raw.
 * @param (Optional) Options object.
 */
create(key: string, value: string, options?: CookieOptions): void;

createObject()

Create a cookie with an object of values

/**
 * @param key ID
 * @param value stored serialized
 * @param (Optional) Options object.
 */
createFromObject(key: string, value: Object, options?: CookieOptions): void;

remove()

Removes specific cookie

/**
 * @param key ID for lookup
 */
remove(key: string): void;

removeAll()

Removes all cookies.

/**
 */
removeAll(): void;

Options

Options object should be a type of CookieOptions interface. The object may have following properties:

  • domain - {string} - The cookie will be available only for this domain and its sub-domains. For security reasons the user agent will not accept the cookie
  • path - {string} - The cookie will be available only for this path and its sub-paths. By default, this is the URL that appears in your <base> tag. if the current domain is not a sub-domain of this domain or equal to it.
  • expires - {string | number | Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT", number of the form milliseconds or minutes or hours or a Date object indicating the exact date/time this cookie will expire.
  • secure - {boolean} - If true, then the cookie will only be available through a secured connection.
  • httpOnly - {boolean} - If true, then the cookie will be set with the HttpOnly flag, and will only be accessible from the remote server. Helps to prevent against XSS attacks.
  • storeUnencoded - {boolean} - If true, then the cookie value will not be encoded and will be stored as provided.
  • sameSite - {'none' | 'lax' | 'strict'} - If strict, then it will prevent the cookie from being sent by the brwoser to the target site in all cross-site browsing context. If lax, then it will provide a reasonable balance between security and usabillity for websites that want to maintain user's logged-in session after the user arrives from an external link.