https://github.com/jigfox/ngu-cookies
https://github.com/jigfox/ngu-cookies
angular angular-library angular-universal cookies
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jigfox/ngu-cookies
- Owner: jigfox
- License: mit
- Created: 2020-03-14T16:15:23.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-06-03T01:59:09.000Z (over 3 years ago)
- Last Synced: 2025-03-11T14:49:31.505Z (10 months ago)
- Topics: angular, angular-library, angular-universal, cookies
- Language: TypeScript
- Size: 956 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://conventionalcommits.org)
[](https://opensource.org/licenses/MIT)
# NguCookies
This is an angular library which provides complete access to cookies in angular universal apps.
## Getting started
### Installation
```sh
npm install ngu-cookies --save
# or
yarn add ngu-cookies
```
### Usage
#### BrowserModule
The `NguCookiesModule` should be imported in your bootstrapped module (i.e. `AppModule`) like this:
```ts
import { NgModule } from '@anular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NguCookiesModule } from 'ngu-cookies';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule, NguCookiesModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppMocule {}
```
It is possible to configure some defaults for writing cookies like this:
```ts
import { NgModule } from '@anular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NguCookiesModule } from 'ngu-cookies';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
NguCookiesModule.withConfig({
domain: '.example.com',
path: '/'
sameSite: 'strict',
secure: true,
maxAge: 10,
expires: new Date(),
skipUriEncoding: true,
}),
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
})
export class AppMocule {}
```
#### ServerModule
To use this library in angular universal you need to add the `NguCookiesBackendModule` to your module definition for the server (i.e. app.server.module.ts) like this:
```ts
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { NguCookiesBackendModule } from 'ngu-cookies';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [AppModule, ServerModule, NguCookiesBackendModule],
bootstrap: [AppComponent],
})
export class AppServerModule {}
```
and you must make sure both `req` and `res` are passed to `.render()` in your server (i.e. `server.ts`) method like this:
```ts
server.get('*', (req, res) => {
res.render(indexHtml, {
req,
res, // <-- this is missing in autogenerated server.ts
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }],
});
});
```
### CookiesService
To access cookies you can `Inject` the `CookiesService` into your `Component`, `Directive`, `Service`, etc like this
```ts
import { Injectable } from '@angular/core';
import { CookiesService } from 'ngu-cookies';
@Injectable()
export class SomeService {
constructor(private cookies: CookiesService) {}
readUserCookie(): string {
return this.cookies.get('user');
}
}
```