An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

![Node.js CI](https://github.com/jigfox/ngu-cookies/workflows/Node.js%20CI/badge.svg)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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');
}
}
```