{"id":13580487,"url":"https://github.com/ngx-utils/cookies","last_synced_at":"2025-04-10T14:23:05.845Z","repository":{"id":22048306,"uuid":"95036761","full_name":"ngx-utils/cookies","owner":"ngx-utils","description":"Manage your cookies on client and server side (Angular Universal)","archived":false,"fork":false,"pushed_at":"2022-06-02T19:54:54.000Z","size":52,"stargazers_count":39,"open_issues_count":22,"forks_count":27,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-21T09:11:30.639Z","etag":null,"topics":["angular","angular-universal","angular4","cookie","cookies"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ngx-utils.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-21T19:01:27.000Z","updated_at":"2024-07-18T12:05:55.000Z","dependencies_parsed_at":"2022-09-01T00:12:10.799Z","dependency_job_id":null,"html_url":"https://github.com/ngx-utils/cookies","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngx-utils%2Fcookies","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngx-utils%2Fcookies/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngx-utils%2Fcookies/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngx-utils%2Fcookies/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngx-utils","download_url":"https://codeload.github.com/ngx-utils/cookies/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248233937,"owners_count":21069493,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angular-universal","angular4","cookie","cookies"],"created_at":"2024-08-01T15:01:52.280Z","updated_at":"2025-04-10T14:23:05.813Z","avatar_url":"https://github.com/ngx-utils.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# @ngx-utils/cookies\n\n[![npm version](https://badge.fury.io/js/%40ngx-utils%2Fcookies.svg)](https://badge.fury.io/js/%40ngx-utils%2Fcookies) [![npm downloads](https://img.shields.io/npm/dm/@ngx-utils/cookies.svg)](https://www.npmjs.com/package/@ngx-utils/cookies)\n\nManage your cookies on client and server side (Angular Universal)\n\nExample in [@ngx-utils/universal-starter](https://github.com/ngx-utils/universal-starter/blob/master/src/app/auth-http.service.ts#L68) 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.\n\n## Table of contents:\n\n- [Prerequisites](#prerequisites)\n- [Getting started](#getting-started)\n  - [Installation](#installation)\n  - [browser.module.ts](#browsermodulets)\n  - [server.module.ts](#servermodulets)\n  - [Cookies options](#cookies-options)\n- [API](#api)\n- [Example of usage](#example-of-usage)\n- [License](#license)\n\n## Prerequisites\n\nThis package depends on `@angular v9.0.0`.\n\n## Getting started\n\n### Installation\n\nInstall **@ngx-utils/cookies** from npm:\n\n```bash\nnpm install @ngx-utils/cookies --save\n```\n\n### browser.module.ts\n\nAdd **BrowserCookiesModule** to your browser module:\n\n```ts\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { BrowserCookiesModule } from '@ngx-utils/cookies/browser';\n...\nimport { AppModule } from './app/app.module';\nimport { AppComponent } from './app/app.component';\n...\n@NgModule({\n  imports: [\n    BrowserModule.withServerTransition({appId: 'your-app-id'}),\n    BrowserCookiesModule.forRoot(),\n    AppModule\n    ...\n  ],\n  bootstrap: [AppComponent]\n})\nexport class BrowserAppModule { }\n```\n\n### server.module.ts\n\nAdd **ServerCookiesModule** to your server module:\n\n```ts\nimport { NgModule } from '@angular/core';\nimport { BrowserModule } from '@angular/platform-browser';\nimport { ServerModule } from '@angular/platform-server';\nimport { ServerCookiesModule } from '@ngx-utils/cookies/server';\n...\nimport { AppModule } from './app/app.module';\nimport { AppComponent } from './app/app.component';\n...\n@NgModule({\n  imports: [\n    BrowserModule.withServerTransition({ appId: 'your-app-id' }),\n    ServerModule,\n    ServerCookiesModule.forRoot(),\n    AppModule\n    ...\n  ],\n  bootstrap: [AppComponent]\n})\nexport class ServerAppModule { }\n```\n\n### Cookies options\n\nYou can preset cookies options:\n\n```ts\nBrowserCookiesModule.forRoot({\n  path: '/',\n  domain: 'your.domain',\n  expires: '01.01.2020',\n  secure: true,\n  httpOnly: true\n})\n...\nServerCookiesModule.forRoot({\n  path: '/',\n  domain: 'your.domain',\n  expires: '01.01.2020',\n  secure: true,\n  httpOnly: true\n})\n```\n\n## API\n\n`CookieService` has following methods:\n\n- `put(key: string, value: string, options?: CookiesOptions): void` put some value to cookies;\n- `putObject(key: string, value: Object, options?: CookiesOptions): void` put object value to cookies;\n- `get(key: string): string` get some value from cookies by `key`;\n- `getObject(key: string): { [key: string]: string } | string` get object value from cookies by `key`;\n- `getAll(): { [key: string]: string }` get all cookies ;\n- `remove(key: string, options?: CookiesOptions): void` remove cookie by `key`;\n- `removeAll(): void` remove all cookies;\n\n## Example of usage\n\nIf you're using `express` as server then add following code to your `server.ts`:\n\n```ts\nimport { renderModuleFactory } from \"@angular/platform-server\";\nimport { provideModuleMap } from \"@nguniversal/module-map-ngfactory-loader\";\nimport * as cookieParser from \"cookie-parser\";\n\napp.use(cookieParser(\"Your private token\"));\n\napp.engine(\"html\", (_, options, callback) =\u003e {\n  renderModuleFactory(AppServerModuleNgFactory, {\n    document: template,\n    url: options.req.url,\n    extraProviders: [\n      provideModuleMap(LAZY_MODULE_MAP),\n      {\n        provide: \"REQUEST\",\n        useValue: options.req\n      },\n      {\n        provide: \"RESPONSE\",\n        useValue: options.req.res\n      }\n    ]\n  }).then(html =\u003e {\n    callback(null, html);\n  });\n});\n```\n\nThen just import `CookiesService` from `@ngx-utils/cookies` and use it:\n\n```ts\nimport { Component, OnInit } from \"@angular/core\";\nimport { CookiesService } from \"@ngx-utils/cookies\";\n\n@Component({\n  selector: \"app-root\",\n  templateUrl: \"./app.component.html\",\n  styleUrls: [\"./app.component.scss\"]\n})\nexport class AppComponent implements OnInit {\n  constructor(private cookies: CookiesService) {}\n\n  ngOnInit() {\n    this.cookies.put(\"some_cookie\", \"some_cookie\");\n    this.cookies.put(\"http_only_cookie\", \"http_only_cookie\", {\n      httpOnly: true\n    });\n    console.log(this.cookies.get(\"some_cookie\"), \" =\u003e some_cookie\");\n    console.log(this.cookies.get(\"http_only_cookie\"), \" =\u003e undefined\");\n    console.log(this.cookies.getAll());\n  }\n}\n```\n\nIf you're using another framework you need to overrride `ServerCookiesService`.\n\nFor example for `koa` you need add following code to your server:\n\n```ts\napp.use(async (ctx: Context) =\u003e {\n  ctx.body = await renderModuleFactory(AppServerModuleNgFactory, {\n    document: template,\n    url: ctx.req.url,\n    extraProviders: [\n      provideModuleMap(LAZY_MODULE_MAP),\n      {\n        provide: \"KOA_CONTEXT\",\n        useValue: ctx\n      }\n    ]\n  });\n});\n```\n\nThen create `server-cookies.service.ts`:\n\n```ts\nimport { Context } from \"koa\";\nimport { Inject, Injectable } from \"@angular/core\";\nimport {\n  CookiesService,\n  CookiesOptionsService,\n  CookiesOptions\n} from \"@ngx-utils/cookies\";\n\n@Injectable()\nexport class ServerCookiesService extends CookiesService {\n  private newCookies: { [name: string]: string | undefined } = {};\n\n  constructor(\n    cookiesOptions: CookiesOptionsService,\n    @Inject(\"KOA_CONTEXT\") private ctx: Context\n  ) {\n    super(cookiesOptions);\n  }\n\n  get(key: string): string {\n    return this.newCookies[key] || this.ctx.cookies.get(key);\n  }\n\n  protected cookiesReader() {\n    return {};\n  }\n\n  protected cookiesWriter(): (\n    name: string,\n    value: string | undefined,\n    options?: CookiesOptions\n  ) =\u003e void {\n    return (name: string, value: string | undefined, options?: any) =\u003e {\n      this.newCookies[name] = value;\n      this.ctx.cookies.set(name, value, { httpOnly: false, ...options });\n    };\n  }\n}\n```\n\nAnd add `server-cookies.service.ts` to `app.server.module.ts`:\n\n```ts\n{\n  provide: CookiesService,\n  useClass: ServerCookiesService,\n},\n```\n\n## License\n\nThe MIT License (MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngx-utils%2Fcookies","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngx-utils%2Fcookies","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngx-utils%2Fcookies/lists"}