All Projects → ngx-utils → cookies

ngx-utils / cookies

Licence: MIT license
Manage your cookies on client and server side (Angular Universal)

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to cookies

CockyGrabber
C# library for the collection of browser information such as cookies, logins, and more
Stars: ✭ 46 (+15%)
Mutual labels:  cookie, cookies
Ngx Cookie Service
Angular (4.2+ ...11) service for cookies. Originally based on the `ng2-cookies` library.
Stars: ✭ 363 (+807.5%)
Mutual labels:  cookie, cookies
Zebra Cookie
A ridiculously small (~500 bytes minified) JavaScript API for writing, reading and deleting browser cookies
Stars: ✭ 15 (-62.5%)
Mutual labels:  cookie, cookies
PolishCookieConsent
Polish Cookie Consent is an extension, which automatically accepts privacy policy/GDPR on websites.
Stars: ✭ 17 (-57.5%)
Mutual labels:  cookie, cookies
cookie-editor
A powerful browser extension to create, edit and delete cookies
Stars: ✭ 245 (+512.5%)
Mutual labels:  cookie, cookies
contao-cookiebar
Display the information about cookies on your Contao website
Stars: ✭ 27 (-32.5%)
Mutual labels:  cookie, cookies
Vue Cookies
A simple Vue.js plugin for handling browser cookies
Stars: ✭ 293 (+632.5%)
Mutual labels:  cookie, cookies
Cookies.js
Simple cookie framework with full Unicode support
Stars: ✭ 254 (+535%)
Mutual labels:  cookie, cookies
Vue Warehouse
A Cross-browser storage for Vue.js and Nuxt.js, with plugins support and easy extensibility based on Store.js.
Stars: ✭ 161 (+302.5%)
Mutual labels:  cookie, cookies
Cookie Autodelete
Firefox and Chrome WebExtension that deletes cookies and other browsing site data as soon as the tab closes, domain changes, browser restarts, or a combination of those events.
Stars: ✭ 1,015 (+2437.5%)
Mutual labels:  cookie, cookies
angular2-cookie-law
Angular2+ component that provides a banner to inform users about cookie law
Stars: ✭ 38 (-5%)
Mutual labels:  cookies, angular4
Client-Storage
🗄 Bulletproof persistent Client storage, works with disabled Cookies and/or localStorage
Stars: ✭ 15 (-62.5%)
Mutual labels:  cookie, cookies
node-ng-ssr-example
Simple Angular Server Side Rendering example
Stars: ✭ 15 (-62.5%)
Mutual labels:  angular4, angular-universal
nginx cookie flag module
Module for Nginx which allows to set the flags "HttpOnly", "secure" and "SameSite" for cookies.
Stars: ✭ 101 (+152.5%)
Mutual labels:  cookie, cookies
Cookie Universal
Universal cookie plugin, perfect for SSR
Stars: ✭ 376 (+840%)
Mutual labels:  cookie, cookies
Meteor-Cookies
🍪 Isomorphic bulletproof cookie functions for client and server
Stars: ✭ 41 (+2.5%)
Mutual labels:  cookie, cookies
cookies
Convenient way to use cookies with PSR-7
Stars: ✭ 17 (-57.5%)
Mutual labels:  cookie, cookies
TypingHero
gary60405.github.io/typinghero/
Stars: ✭ 28 (-30%)
Mutual labels:  angular4
http
Basic HTTP primitives which can be shared by servers and clients.
Stars: ✭ 75 (+87.5%)
Mutual labels:  cookie
KaufmannDigital.GDPR.CookieConsent
A ready-to-run package, that integrates an advanced cookie consent banner into your Neos CMS site.
Stars: ✭ 21 (-47.5%)
Mutual labels:  cookies

@ngx-utils/cookies

npm version npm downloads

Manage your cookies on client and server side (Angular Universal)

Example in @ngx-utils/universal-starter shows the way in which CookiesService is used to get access token from cookies on client and server side, and then set Authorization headers for all HTTP requests.

Table of contents:

Prerequisites

This package depends on @angular v9.0.0.

Getting started

Installation

Install @ngx-utils/cookies from npm:

npm install @ngx-utils/cookies --save

browser.module.ts

Add BrowserCookiesModule to your browser module:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserCookiesModule } from '@ngx-utils/cookies/browser';
...
import { AppModule } from './app/app.module';
import { AppComponent } from './app/app.component';
...
@NgModule({
  imports: [
    BrowserModule.withServerTransition({appId: 'your-app-id'}),
    BrowserCookiesModule.forRoot(),
    AppModule
    ...
  ],
  bootstrap: [AppComponent]
})
export class BrowserAppModule { }

server.module.ts

Add ServerCookiesModule to your server module:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ServerModule } from '@angular/platform-server';
import { ServerCookiesModule } from '@ngx-utils/cookies/server';
...
import { AppModule } from './app/app.module';
import { AppComponent } from './app/app.component';
...
@NgModule({
  imports: [
    BrowserModule.withServerTransition({ appId: 'your-app-id' }),
    ServerModule,
    ServerCookiesModule.forRoot(),
    AppModule
    ...
  ],
  bootstrap: [AppComponent]
})
export class ServerAppModule { }

Cookies options

You can preset cookies options:

BrowserCookiesModule.forRoot({
  path: '/',
  domain: 'your.domain',
  expires: '01.01.2020',
  secure: true,
  httpOnly: true
})
...
ServerCookiesModule.forRoot({
  path: '/',
  domain: 'your.domain',
  expires: '01.01.2020',
  secure: true,
  httpOnly: true
})

API

CookieService has following methods:

  • put(key: string, value: string, options?: CookiesOptions): void put some value to cookies;
  • putObject(key: string, value: Object, options?: CookiesOptions): void put object value to cookies;
  • get(key: string): string get some value from cookies by key;
  • getObject(key: string): { [key: string]: string } | string get object value from cookies by key;
  • getAll(): { [key: string]: string } get all cookies ;
  • remove(key: string, options?: CookiesOptions): void remove cookie by key;
  • removeAll(): void remove all cookies;

Example of usage

If you're using express as server then add following code to your server.ts:

import { renderModuleFactory } from "@angular/platform-server";
import { provideModuleMap } from "@nguniversal/module-map-ngfactory-loader";
import * as cookieParser from "cookie-parser";

app.use(cookieParser("Your private token"));

app.engine("html", (_, options, callback) => {
  renderModuleFactory(AppServerModuleNgFactory, {
    document: template,
    url: options.req.url,
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP),
      {
        provide: "REQUEST",
        useValue: options.req
      },
      {
        provide: "RESPONSE",
        useValue: options.req.res
      }
    ]
  }).then(html => {
    callback(null, html);
  });
});

Then just import CookiesService from @ngx-utils/cookies and use it:

import { Component, OnInit } from "@angular/core";
import { CookiesService } from "@ngx-utils/cookies";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent implements OnInit {
  constructor(private cookies: CookiesService) {}

  ngOnInit() {
    this.cookies.put("some_cookie", "some_cookie");
    this.cookies.put("http_only_cookie", "http_only_cookie", {
      httpOnly: true
    });
    console.log(this.cookies.get("some_cookie"), " => some_cookie");
    console.log(this.cookies.get("http_only_cookie"), " => undefined");
    console.log(this.cookies.getAll());
  }
}

If you're using another framework you need to overrride ServerCookiesService.

For example for koa you need add following code to your server:

app.use(async (ctx: Context) => {
  ctx.body = await renderModuleFactory(AppServerModuleNgFactory, {
    document: template,
    url: ctx.req.url,
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP),
      {
        provide: "KOA_CONTEXT",
        useValue: ctx
      }
    ]
  });
});

Then create server-cookies.service.ts:

import { Context } from "koa";
import { Inject, Injectable } from "@angular/core";
import {
  CookiesService,
  CookiesOptionsService,
  CookiesOptions
} from "@ngx-utils/cookies";

@Injectable()
export class ServerCookiesService extends CookiesService {
  private newCookies: { [name: string]: string | undefined } = {};

  constructor(
    cookiesOptions: CookiesOptionsService,
    @Inject("KOA_CONTEXT") private ctx: Context
  ) {
    super(cookiesOptions);
  }

  get(key: string): string {
    return this.newCookies[key] || this.ctx.cookies.get(key);
  }

  protected cookiesReader() {
    return {};
  }

  protected cookiesWriter(): (
    name: string,
    value: string | undefined,
    options?: CookiesOptions
  ) => void {
    return (name: string, value: string | undefined, options?: any) => {
      this.newCookies[name] = value;
      this.ctx.cookies.set(name, value, { httpOnly: false, ...options });
    };
  }
}

And add server-cookies.service.ts to app.server.module.ts:

{
  provide: CookiesService,
  useClass: ServerCookiesService,
},

License

The MIT License (MIT)

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].