All Projects → salemdar → Ngx Cookie

salemdar / Ngx Cookie

Licence: mit
Implementation of Angular 1.x $cookies service to Angular 2

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Cookie

nginx cookie flag module
Module for Nginx which allows to set the flags "HttpOnly", "secure" and "SameSite" for cookies.
Stars: ✭ 101 (-61.89%)
Mutual labels:  cookie
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (-58.11%)
Mutual labels:  cookie
gdpr-cookie
Php Cookie checker for Analytics and Tawk.To (GDPR Compliance)
Stars: ✭ 21 (-92.08%)
Mutual labels:  cookie
next-fauna-auth
Implemented cookie-based user authentication in a Next.js, FaunaDB and GraphQL app
Stars: ✭ 29 (-89.06%)
Mutual labels:  cookie
javascript-cookie-control
Javascript module for controling cookie consent on your website
Stars: ✭ 18 (-93.21%)
Mutual labels:  cookie
Zebra Cookie
A ridiculously small (~500 bytes minified) JavaScript API for writing, reading and deleting browser cookies
Stars: ✭ 15 (-94.34%)
Mutual labels:  cookie
yii2-cookiemonster
Yii extension to manage cookie warning
Stars: ✭ 16 (-93.96%)
Mutual labels:  cookie
Cookies.js
Simple cookie framework with full Unicode support
Stars: ✭ 254 (-4.15%)
Mutual labels:  cookie
nestjs-cookie-session
Idiomatic Cookie Session Module for NestJS. Built on top of `cookie-session` 😻
Stars: ✭ 35 (-86.79%)
Mutual labels:  cookie
facebook-tool-seller
Facebook Tool Seller Version 1.0.1
Stars: ✭ 25 (-90.57%)
Mutual labels:  cookie
summary1
个人总结 持续更新 欢迎提出各种issues
Stars: ✭ 13 (-95.09%)
Mutual labels:  cookie
CockyGrabber
C# library for the collection of browser information such as cookies, logins, and more
Stars: ✭ 46 (-82.64%)
Mutual labels:  cookie
node-fetch-cookies
node-fetch wrapper that adds support for cookie-jars
Stars: ✭ 15 (-94.34%)
Mutual labels:  cookie
micell
A collection of functions for front-end development
Stars: ✭ 16 (-93.96%)
Mutual labels:  cookie
vue-cookie-next
A vue 3 plugin for handling browser cookies with typescript support. Load and save cookies within your Vue 3 application
Stars: ✭ 37 (-86.04%)
Mutual labels:  cookie
contao-cookiebar
Display the information about cookies on your Contao website
Stars: ✭ 27 (-89.81%)
Mutual labels:  cookie
ItroublveTSC
Official Source of ItroublveTSC, totally open source. No virus or anything. Feel free to have a look :)
Stars: ✭ 82 (-69.06%)
Mutual labels:  cookie
Flask Session Cookie Manager
🍪 Flask Session Cookie Decoder/Encoder
Stars: ✭ 257 (-3.02%)
Mutual labels:  cookie
typo3-dp cookieconsent
TYPO3 Extension: Enable a cookie consent box. Let you visitors control the usage of cookies and load script or content after a consent. (ePrivacy, TTDSG)
Stars: ✭ 28 (-89.43%)
Mutual labels:  cookie
cordova-plugin-wkwebview-inject-cookie
Injects a cookie in order to start the sync processs with wkWebView
Stars: ✭ 43 (-83.77%)
Mutual labels:  cookie

ngx-cookie CI npm version Downloads Codacy Badge

Implementation of Angular 1.x $cookies service to Angular

Table of contents:

Get Started

Installation

You can install this package locally with npm.

# To get the latest stable version and update package.json file:
yarn add ngx-cookie
# or
# npm install ngx-cookie --save

Usage

CookieModule should be registered in the AppModule with forRoot() static method and with forChild() in the child modules. These methods accept 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';

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

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

@Component({
    selector: 'my-very-cool-app',
    template: '<h1>My Angular App with Cookies</h1>'
})

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

Server Side Rendering

ngx-cookie supports usage during Server Side Rendering (SSR / Angular Universal). Getting Server Side Rendering itself set up the first time can be tricky and is outside the scope of this guide. Here, we'll assume that you've got a working SSR setup similar to the Angular Universal Starter project, and you're just trying to get ngx-cookie working with SSR.

Note: during normal, client side usage, ngx-cookie manipulates the client cookies attached to the document object. During SSR, ngx-cookie will manipulate cookies in http request or response headers._

Setup

Install ngx-cookie-backend library:

yarn add ngx-cookie-backend
# or
# npm install ngx-cookie-backend --save

Then edit app.server.module.ts and add CookieBackendModule.forRoot() to imports:

/* app.server.module.ts */

import { CookieBackendModule } from 'ngx-cookie-backend';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    CookieBackendModule.forRoot()
  ],
  bootstrap: [AppComponent]
})
export class AppServerModule {}

Next, we need to make providers for the 'REQUEST' and 'RESPONSE' objects created by the expressjs server during SSR. To do this, edit server.ts to create providers for 'REQUEST' AND 'RESPONSE'.

/* server.ts */
// All regular routes use the Universal engine
server.get('*', (req, res) => {
  res.render(indexHtml, {
    req,
    res,
    providers: [
      {provide: APP_BASE_HREF, useValue: req.baseUrl},
      {provide: REQUEST, useValue: req},
      {provide: RESPONSE, useValue: res}
    ]
  });
});

And that's it! all your application's calls to CookieService should now work properly during SSR!

Examples

Normal usage example is under projects/test-app

SSR usage example is under projects/backend-test-app

CookieService

There are 7 methods available in the CookieService (6 standard methods from Angular 1 and 1 extra removeAll() method for convenience)

get()

Returns the value of given cookie key.

/**
 * @param {string} key Id to use for lookup.
 * @returns {string} Raw cookie value.
 */
get(key: string): string;

getObject()

Returns the deserialized value of given cookie key.

/**
 * @param {string} key Id to use for lookup.
 * @returns {Object} Deserialized cookie value.
 */
getObject(key: string): Object;

getAll()

Returns a key value object with all the cookies.

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

put()

Sets a value for given cookie key.

/**
 * @param {string} key Id for the `value`.
 * @param {string} value Raw value to be stored.
 * @param {CookieOptions} options (Optional) Options object.
 */
put(key: string, value: string, options?: CookieOptions): void;

putObject()

Serializes and sets a value for given cookie key.

/**
 * @param {string} key Id for the `value`.
 * @param {Object} value Value to be stored.
 * @param {CookieOptions} options (Optional) Options object.
 */
putObject(key: string, value: Object, options?: CookieOptions): void;

remove()

Remove given cookie.

/**
 * @param {string} key Id of the key-value pair to delete.
 * @param {CookieOptions} options (Optional) Options object.
 */
remove(key: string, options?: CookieOptions): void;

removeAll()

Remove all cookies.

/**
 */
removeAll(): void;

Options

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

  • 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.
  • 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 if the current domain is not a sub-domain of this domain or equal to it.
  • expires - {string|Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT" 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.
  • sameSite - {"Lax"|"Strict"|"None"} - Designates cookie for first party (Lax|Strict) or third party contexts.
  • 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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].