Skip to content

Commit 38c4919

Browse files
author
Anton Barada
committed
update to angular 5
1 parent 51bfa3a commit 38c4919

File tree

3 files changed

+1818
-205
lines changed

3 files changed

+1818
-205
lines changed

README.md

Lines changed: 131 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,38 @@ Manage your cookies on client and server side (Angular Universal)
77
Example in [@ngx-utils/universal-starter](https://github.com/ngx-utils/universal-starter/blob/master/src/app/auth-http.service.ts#L68) 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.
88

99
## Table of contents:
10-
- [Prerequisites](#prerequisites)
11-
- [Getting started](#getting-started)
12-
- [Installation](#installation)
13-
- [browser.module.ts](#browsermodulets)
14-
- [server.module.ts](#servermodulets)
15-
- [Cookies options](#cookies-options)
16-
- [API](#api)
17-
- [Example of usage](#example-of-usage)
18-
- [License](#license)
1910

20-
## Prerequisites
21-
22-
This package depends on `@angular v4.0.0`, `@ngx-utils/express-engine` and `cookie-parser`.
23-
24-
Install `@ngx-utils/express-engine` and `cookie-parser` from npm:
25-
```bash
26-
npm install @ngx-utils/express-engine cookie-parser --save
27-
```
28-
29-
And add cookie parser middlewear to you **server.ts** that should looks like this:
30-
```ts
31-
import 'zone.js/dist/zone-node';
32-
import 'reflect-metadata';
33-
import * as express from 'express';
34-
import * as cookieParser from 'cookie-parser';
35-
import { enableProdMode } from '@angular/core';
36-
import { ngExpressEngine } from '@ngx-utils/express-engine';
37-
38-
import { ServerAppModuleNgFactory } from './ngfactory/server.module.ngfactory';
39-
import { environment } from './environments/environment';
40-
41-
const app = express();
42-
43-
enableProdMode();
44-
45-
app.use(cookieParser('Your private token'));
46-
47-
app.engine('html', ngExpressEngine({
48-
aot: true,
49-
bootstrap: ServerAppModuleNgFactory
50-
}));
11+
* [Prerequisites](#prerequisites)
12+
* [Getting started](#getting-started)
13+
* [Installation](#installation)
14+
* [browser.module.ts](#browsermodulets)
15+
* [server.module.ts](#servermodulets)
16+
* [Cookies options](#cookies-options)
17+
* [API](#api)
18+
* [Example of usage](#example-of-usage)
19+
* [License](#license)
5120

52-
app.set('view engine', 'html');
53-
app.set('views', 'dist/client');
54-
55-
app.get('*', (req, res) => {
56-
res.render('../client/index', {cache: true, req, res});
57-
});
21+
## Prerequisites
5822

59-
app.listen(environment.port);
23+
This package depends on `@angular v5.0.0`.
6024

61-
```
25+
And if you want to manage cookies on server side and you're using express as server you need install:
26+
`npm i -S cookie-parser @nguniversal/module-map-ngfactory-loader`
6227

6328
## Getting started
6429

6530
### Installation
6631

6732
Install **@ngx-utils/cookies** from npm:
33+
6834
```bash
6935
npm install @ngx-utils/cookies --save
7036
```
7137

7238
### browser.module.ts
7339

7440
Add **BrowserCookiesModule** to your browser module:
41+
7542
```ts
7643
import { NgModule } from '@angular/core';
7744
import { BrowserModule } from '@angular/platform-browser';
@@ -90,12 +57,12 @@ import { AppComponent } from './app/app.component';
9057
bootstrap: [AppComponent]
9158
})
9259
export class BrowserAppModule { }
93-
9460
```
9561

9662
### server.module.ts
9763

9864
Add **ServerCookiesModule** to your server module:
65+
9966
```ts
10067
import { NgModule } from '@angular/core';
10168
import { BrowserModule } from '@angular/platform-browser';
@@ -116,12 +83,12 @@ import { AppComponent } from './app/app.component';
11683
bootstrap: [AppComponent]
11784
})
11885
export class ServerAppModule { }
119-
12086
```
12187

12288
### Cookies options
12389

12490
You can preset cookies options:
91+
12592
```ts
12693
BrowserCookiesModule.forRoot({
12794
path: '/',
@@ -143,20 +110,51 @@ ServerCookiesModule.forRoot({
143110
## API
144111

145112
`CookieService` has following methods:
146-
- `put(key: string, value: string, options?: CookiesOptions): void` put some value to cookies;
147-
- `putObject(key: string, value: Object, options?: CookiesOptions): void` put object value to cookies;
148-
- `get(key: string): string` get some value from cookies by `key`;
149-
- `getObject(key: string): { [key: string]: string } | string` get object value from cookies by `key`;
150-
- `getAll(): { [key: string]: string }` get all cookies ;
151-
- `remove(key: string, options?: CookiesOptions): void` remove cookie by `key`;
152-
- `removeAll(): void` remove all cookies;
113+
114+
* `put(key: string, value: string, options?: CookiesOptions): void` put some value to cookies;
115+
* `putObject(key: string, value: Object, options?: CookiesOptions): void` put object value to cookies;
116+
* `get(key: string): string` get some value from cookies by `key`;
117+
* `getObject(key: string): { [key: string]: string } | string` get object value from cookies by `key`;
118+
* `getAll(): { [key: string]: string }` get all cookies ;
119+
* `remove(key: string, options?: CookiesOptions): void` remove cookie by `key`;
120+
* `removeAll(): void` remove all cookies;
153121

154122
## Example of usage
155123

156-
Just import `CookiesService` from `@ngx-utils/cookies` and use it:
124+
If you're using `express` as server then add following code to your `server.ts`:
125+
126+
```ts
127+
import { renderModuleFactory } from '@angular/platform-server';
128+
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
129+
import * as cookieParser from 'cookie-parser';
130+
131+
app.use(cookieParser('Your private token'));
132+
133+
app.engine('html', (_, options, callback) => {
134+
renderModuleFactory(AppServerModuleNgFactory, {
135+
document: template,
136+
url: options.req.url,
137+
extraProviders: [
138+
provideModuleMap(LAZY_MODULE_MAP),
139+
{
140+
provide: 'REQUEST',
141+
useValue: options.req
142+
},
143+
{
144+
provide: 'RESPONSE',
145+
useValue: options.req.res
146+
}
147+
]
148+
}).then(html => {
149+
callback(null, html);
150+
});
151+
});
152+
```
153+
154+
Then just import `CookiesService` from `@ngx-utils/cookies` and use it:
157155

158156
```ts
159-
import { Component, OnInit} from '@angular/core';
157+
import { Component, OnInit } from '@angular/core';
160158
import { CookiesService } from '@ngx-utils/cookies';
161159

162160
@Component({
@@ -165,18 +163,90 @@ import { CookiesService } from '@ngx-utils/cookies';
165163
styleUrls: ['./app.component.scss']
166164
})
167165
export class AppComponent implements OnInit {
168-
constructor(private cookies: CookiesService) { }
166+
constructor(private cookies: CookiesService) {}
169167

170168
ngOnInit() {
171169
this.cookies.put('some_cookie', 'some_cookie');
172-
this.cookies.put('http_only_cookie', 'http_only_cookie', {httpOnly: true});
170+
this.cookies.put('http_only_cookie', 'http_only_cookie', {
171+
httpOnly: true
172+
});
173173
console.log(this.cookies.get('some_cookie'), ' => some_cookie');
174174
console.log(this.cookies.get('http_only_cookie'), ' => undefined');
175175
console.log(this.cookies.getAll());
176176
}
177177
}
178+
```
179+
180+
If you're using another framework you need to overrride `ServerCookiesService`.
181+
182+
For example for `koa` you need add following code to your server:
183+
184+
```ts
185+
app.use(async (ctx: Context) => {
186+
ctx.body = await renderModuleFactory(AppServerModuleNgFactory, {
187+
document: template,
188+
url: ctx.req.url,
189+
extraProviders: [
190+
provideModuleMap(LAZY_MODULE_MAP),
191+
{
192+
provide: 'KOA_CONTEXT',
193+
useValue: ctx
194+
}
195+
]
196+
});
197+
});
198+
```
178199

200+
Then create `server-cookies.service.ts`:
179201

202+
```ts
203+
import { Context } from 'koa';
204+
import { Inject, Injectable } from '@angular/core';
205+
import {
206+
CookiesService,
207+
CookiesOptionsService,
208+
CookiesOptions
209+
} from '@ngx-utils/cookies';
210+
211+
@Injectable()
212+
export class ServerCookiesService extends CookiesService {
213+
private newCookies: { [name: string]: string | undefined } = {};
214+
215+
constructor(
216+
cookiesOptions: CookiesOptionsService,
217+
@Inject('KOA_CONTEXT') private ctx: Context
218+
) {
219+
super(cookiesOptions);
220+
}
221+
222+
get(key: string): string {
223+
return this.newCookies[key] || this.ctx.cookies.get(key);
224+
}
225+
226+
protected cookiesReader() {
227+
return {};
228+
}
229+
230+
protected cookiesWriter(): (
231+
name: string,
232+
value: string | undefined,
233+
options?: CookiesOptions
234+
) => void {
235+
return (name: string, value: string | undefined, options?: any) => {
236+
this.newCookies[name] = value;
237+
this.ctx.cookies.set(name, value, { httpOnly: false, ...options });
238+
};
239+
}
240+
}
241+
```
242+
243+
And add `server-cookies.service.ts` to `app.server.module.ts`:
244+
245+
```ts
246+
{
247+
provide: CookiesService,
248+
useClass: ServerCookiesService,
249+
},
180250
```
181251

182252
## License

0 commit comments

Comments
 (0)