Gathering detailed insights and metrics for ngx-cookies-next
Gathering detailed insights and metrics for ngx-cookies-next
npm install ngx-cookies-next
Typescript
Module System
Node Version
NPM Version
68.5
Supply Chain
96.2
Quality
75
Maintenance
50
Vulnerability
98.9
License
TypeScript (97.19%)
JavaScript (2.81%)
Total Downloads
1,650
Last Day
1
Last Week
3
Last Month
9
Last Year
76
39 Stars
27 Commits
27 Forks
2 Watching
6 Branches
2 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
3
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.
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
Install @ngx-utils/cookies from npm:
1npm install @ngx-utils/cookies --save
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 { }
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 { }
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})
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;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},
The MIT License (MIT)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
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
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
14 existing vulnerabilities detected
Details
Score
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