![38% of CISOs Fear They’re Not Moving Fast Enough on AI](https://cdn.sanity.io/images/cgdhsj6q/production/faa0bc28df98f791e11263f8239b34207f84b86f-1024x1024.webp?w=400&fit=max&auto=format)
Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@hallysonh/ngx-cookie
Advanced tools
This project is a Angular 6+ version of the original ngx-cookie. Use it just until the original project gets Angular 6+ support.
Implementation of Angular 1.x $cookies service to Angular. Successor of angular2-cookie
You can install this package locally with npm.
# To get the latest stable version and update package.json file:
npm install @hallysonh/ngx-cookie --save
# or
# yarn add @hallysonh/ngx-cookie
After installing the library, it should be included in the SystemJS configurations.
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
* Taken from: https://github.com/angular/quickstart/blob/master/systemjs.config.js
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'@hallysonh/ngx-cookie': 'npm:@hallysonh/ngx-cookie/bundles/hallysonh-ngx-cookie.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
CookieModule
should be registered in the AppModule
with forRoot()
static method and with forChild()
in the child modules.
These methods accepts CookieOptions
objects as well. Leave it blank for the defaults.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CookieModule } from '@hallysonh/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 '@hallysonh/ngx-cookie';
@Component({
selector: 'my-very-cool-app',
template: '<h1>My Angular2 App with Cookies</h1>'
})
export class AppComponent {
constructor(private _cookieService:CookieService){}
getCookie(key: string){
return this._cookieService.get(key);
}
}
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.
First up, edit app.server.module.ts
(located in root > src > app
of the Universal starter project) to overwrite ngx-cookie's CookieService
with ngx-cookie's CookieBackendService
during server side rendering.
/* app.server.module.ts */
import { CookieService, CookieBackendService } from '@hallysonh/ngx-cookie';
@NgModule({
imports: [
AppModule,
ServerModule,
ModuleMapLoaderModule,
],
bootstrap: [AppComponent],
providers: [{ provide: CookieService, useClass: CookieBackendService }], // <--- CHANGES * * * * *
})
export class AppServerModule {}
Next, we need to make providers for the 'REQUEST'
and 'RESPONSE'
objects created by the expressjs server during SSR. You can check out the CookieBackendService
code, but during SSR ngx-cookie
inject's these objects into CookieBackendService
. To do this, edit server.ts
(located in the root of the Universal Starter Project) to create providers for 'REQUEST'
AND 'RESPONSE'
.
/* server.ts */
// Find the call to res.render() in the file and
// update it with providers for 'REQUEST' and 'RESPONSE'
app.get('*', (req, res) => {
res.render('index', {
req: req,
res: res,
providers: [
{
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!
Here you can find some usage examples with popular boilerplate libraries.
A boilerplate provided by Angular team. (Link: https://github.com/angular/quickstart)
Just edit the systemjs.config.js
file and add the @hallysonh/ngx-cookie
there.
/**
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'@hallysonh/ngx-cookie': 'npm:@hallysonh/ngx-cookie/bundles/hallysonh-ngx-cookie.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
A popular seed project. (Link: https://github.com/mgechev/angular2-seed)
Add the following settings to the (constructor of) ProjectConfig
class (path: ./tools/config/project.config.ts
).
let additionalPackages: ExtendPackages[] = [{
name: '@hallysonh/ngx-cookie',
// Path to the package's bundle
path: 'node_modules/@hallysonh/ngx-cookie/bundles/hallysonh-ngx-cookie.umd.js'
}];
this.addPackagesBundles(additionalPackages);
Do not forget to inject the CookieModule
in the module AppModule
and SharedModule
.
There are 7 methods available in the CookieService
(6 standard methods from Angular 1 and 1 extra removeAll()
method for convenience)
Returns the value of given cookie key.
/**
* @param {string} key Id to use for lookup.
* @returns {string} Raw cookie value.
*/
get(key: string): string;
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;
Returns a key value object with all the cookies.
/**
* @returns {Object} All cookies
*/
getAll(): any;
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;
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 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;
Remove all cookies.
/**
*/
removeAll(): void;
Options object should be a type of CookieOptions
interface. The object may have following properties:
<base>
tag.true
, then the cookie will only be available through a secured connection.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.true
, then the cookie value will not be encoded and will be stored as provided.FAQs
Implementation of Angular 1.x $cookies service to Angular
We found that @hallysonh/ngx-cookie demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.