Installations
npm install ngx-cookies-next
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
9.11.1
NPM Version
5.6.0
Score
68.5
Supply Chain
96.2
Quality
75
Maintenance
50
Vulnerability
98.9
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (97.19%)
JavaScript (2.81%)
Developer
ngx-utils
Download Statistics
Total Downloads
1,650
Last Day
1
Last Week
3
Last Month
9
Last Year
76
GitHub Statistics
39 Stars
27 Commits
27 Forks
2 Watching
6 Branches
2 Contributors
Bundle Size
1.94 kB
Minified
782.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.0.2
Package Id
ngx-cookies-next@3.0.2
Unpacked Size
72.87 kB
Size
16.75 kB
File Count
76
NPM Version
5.6.0
Node Version
9.11.1
Total Downloads
Cumulative downloads
Total Downloads
1,650
Last day
0%
1
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
200%
9
Compared to previous month
Last year
4.1%
76
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
@ngx-utils/cookies
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 v5.0.0
.
And if you want to manage cookies on server side and you're using express as server you need install:
npm i -S cookie-parser @nguniversal/module-map-ngfactory-loader
Getting started
Installation
Install @ngx-utils/cookies from npm:
1npm install @ngx-utils/cookies --save
browser.module.ts
Add BrowserCookiesModule to your browser module:
1import { NgModule } from '@angular/core'; 2import { BrowserModule } from '@angular/platform-browser'; 3import { BrowserCookiesModule } from '@ngx-utils/cookies/browser'; 4... 5import { AppModule } from './app/app.module'; 6import { AppComponent } from './app/app.component'; 7... 8@NgModule({ 9 imports: [ 10 BrowserModule.withServerTransition({appId: 'your-app-id'}), 11 BrowserCookiesModule.forRoot(), 12 AppModule 13 ... 14 ], 15 bootstrap: [AppComponent] 16}) 17export class BrowserAppModule { }
server.module.ts
Add ServerCookiesModule to your server module:
1import { NgModule } from '@angular/core'; 2import { BrowserModule } from '@angular/platform-browser'; 3import { ServerModule } from '@angular/platform-server'; 4import { ServerCookiesModule } from '@ngx-utils/cookies/server'; 5... 6import { AppModule } from './app/app.module'; 7import { AppComponent } from './app/app.component'; 8... 9@NgModule({ 10 imports: [ 11 BrowserModule.withServerTransition({ appId: 'your-app-id' }), 12 ServerModule, 13 ServerCookiesModule.forRoot(), 14 AppModule 15 ... 16 ], 17 bootstrap: [AppComponent] 18}) 19export class ServerAppModule { }
Cookies options
You can preset cookies options:
1BrowserCookiesModule.forRoot({
2 path: '/',
3 domain: 'your.domain',
4 expires: '01.01.2020',
5 secure: true,
6 httpOnly: true
7})
8...
9ServerCookiesModule.forRoot({
10 path: '/',
11 domain: 'your.domain',
12 expires: '01.01.2020',
13 secure: true,
14 httpOnly: true
15})
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 bykey
;getObject(key: string): { [key: string]: string } | string
get object value from cookies bykey
;getAll(): { [key: string]: string }
get all cookies ;remove(key: string, options?: CookiesOptions): void
remove cookie bykey
;removeAll(): void
remove all cookies;
Example of usage
If you're using express
as server then add following code to your server.ts
:
1import { renderModuleFactory } from '@angular/platform-server'; 2import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; 3import * as cookieParser from 'cookie-parser'; 4 5app.use(cookieParser('Your private token')); 6 7app.engine('html', (_, options, callback) => { 8 renderModuleFactory(AppServerModuleNgFactory, { 9 document: template, 10 url: options.req.url, 11 extraProviders: [ 12 provideModuleMap(LAZY_MODULE_MAP), 13 { 14 provide: 'REQUEST', 15 useValue: options.req 16 }, 17 { 18 provide: 'RESPONSE', 19 useValue: options.req.res 20 } 21 ] 22 }).then(html => { 23 callback(null, html); 24 }); 25});
Then just import CookiesService
from @ngx-utils/cookies
and use it:
1import { Component, OnInit } from '@angular/core'; 2import { CookiesService } from '@ngx-utils/cookies'; 3 4@Component({ 5 selector: 'app-root', 6 templateUrl: './app.component.html', 7 styleUrls: ['./app.component.scss'] 8}) 9export class AppComponent implements OnInit { 10 constructor(private cookies: CookiesService) {} 11 12 ngOnInit() { 13 this.cookies.put('some_cookie', 'some_cookie'); 14 this.cookies.put('http_only_cookie', 'http_only_cookie', { 15 httpOnly: true 16 }); 17 console.log(this.cookies.get('some_cookie'), ' => some_cookie'); 18 console.log(this.cookies.get('http_only_cookie'), ' => undefined'); 19 console.log(this.cookies.getAll()); 20 } 21}
If you're using another framework you need to overrride ServerCookiesService
.
For example for koa
you need add following code to your server:
1app.use(async (ctx: Context) => {
2 ctx.body = await renderModuleFactory(AppServerModuleNgFactory, {
3 document: template,
4 url: ctx.req.url,
5 extraProviders: [
6 provideModuleMap(LAZY_MODULE_MAP),
7 {
8 provide: 'KOA_CONTEXT',
9 useValue: ctx
10 }
11 ]
12 });
13});
Then create server-cookies.service.ts
:
1import { Context } from 'koa'; 2import { Inject, Injectable } from '@angular/core'; 3import { 4 CookiesService, 5 CookiesOptionsService, 6 CookiesOptions 7} from '@ngx-utils/cookies'; 8 9@Injectable() 10export class ServerCookiesService extends CookiesService { 11 private newCookies: { [name: string]: string | undefined } = {}; 12 13 constructor( 14 cookiesOptions: CookiesOptionsService, 15 @Inject('KOA_CONTEXT') private ctx: Context 16 ) { 17 super(cookiesOptions); 18 } 19 20 get(key: string): string { 21 return this.newCookies[key] || this.ctx.cookies.get(key); 22 } 23 24 protected cookiesReader() { 25 return {}; 26 } 27 28 protected cookiesWriter(): ( 29 name: string, 30 value: string | undefined, 31 options?: CookiesOptions 32 ) => void { 33 return (name: string, value: string | undefined, options?: any) => { 34 this.newCookies[name] = value; 35 this.ctx.cookies.set(name, value, { httpOnly: false, ...options }); 36 }; 37 } 38}
And add server-cookies.service.ts
to app.server.module.ts
:
1{ 2 provide: CookiesService, 3 useClass: ServerCookiesService, 4},
License
The MIT License (MIT)
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/25 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-c75v-2vq8-878f
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More