1 | <p align="center">
|
2 | <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
|
3 | </p>
|
4 |
|
5 | [travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
|
6 | [travis-url]: https://travis-ci.org/nestjs/nest
|
7 | [linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
|
8 | [linux-url]: https://travis-ci.org/nestjs/nest
|
9 |
|
10 | <p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
|
11 | <p align="center">
|
12 | <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
13 | <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
14 | <a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
|
15 | <a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a>
|
16 | <a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a>
|
17 | <a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a>
|
18 | <a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a>
|
19 | <a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
20 | <a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
21 | <a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a>
|
22 | <a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
|
23 | </p>
|
24 | |
25 | [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
|
26 |
|
27 | ## Description
|
28 |
|
29 | JWT utilities module for [Nest](https://github.com/nestjs/nest) based on the [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) package.
|
30 |
|
31 | ## Installation
|
32 |
|
33 | ```bash
|
34 | $ npm i --save @nestjs/jwt
|
35 | ```
|
36 |
|
37 | ## Usage
|
38 |
|
39 | Import `JwtModule`:
|
40 |
|
41 | ```typescript
|
42 | @Module({
|
43 | imports: [JwtModule.register({ secretOrPrivateKey: 'key' })],
|
44 | providers: [...],
|
45 | })
|
46 | export class AuthModule {}
|
47 | ```
|
48 |
|
49 | Inject `JwtService`:
|
50 |
|
51 | ```typescript
|
52 | @Injectable()
|
53 | export class AuthService {
|
54 | constructor(private readonly jwtService: JwtService) {}
|
55 | }
|
56 | ```
|
57 |
|
58 | ## Async options
|
59 |
|
60 | Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use `registerAsync()` method, that provides a couple of various ways to deal with async data.
|
61 |
|
62 |
|
63 | **1. Use factory**
|
64 | ```typescript
|
65 | JwtModule.registerAsync({
|
66 | useFactory: () => ({
|
67 | secretOrPrivateKey: 'key',
|
68 | }),
|
69 | })
|
70 | ```
|
71 | Obviously, our factory behaves like every other one (might be `async` and is able to inject dependencies through `inject`).
|
72 |
|
73 | ```typescript
|
74 | JwtModule.registerAsync({
|
75 | imports: [ConfigModule],
|
76 | useFactory: async (configService: ConfigService) => ({
|
77 | secretOrPrivateKey: configService.getString('SECRET_KEY'),
|
78 | }),
|
79 | inject: [ConfigService],
|
80 | }),
|
81 | ```
|
82 |
|
83 | **2. Use class**
|
84 | ```typescript
|
85 | JwtModule.registerAsync({
|
86 | useClass: JwtConfigService,
|
87 | })
|
88 | ```
|
89 | Above construction will instantiate `JwtConfigService` inside `JwtModule` and will leverage it to create options object.
|
90 | ```typescript
|
91 | class JwtConfigService implements JwtOptionsFactory {
|
92 | createJwtOptions(): JwtModuleOptions {
|
93 | return {
|
94 | secretOrPrivateKey: 'key',
|
95 | };
|
96 | }
|
97 | }
|
98 | ```
|
99 |
|
100 | **3. Use existing**
|
101 | ```typescript
|
102 | JwtModule.registerAsync({
|
103 | imports: [ConfigModule],
|
104 | useExisting: ConfigService,
|
105 | }),
|
106 | ```
|
107 | It works the same as `useClass` with one critical difference - `JwtModule` will lookup imported modules to reuse already created `ConfigService`, instead of instantiating it on its own.
|
108 |
|
109 |
|
110 | ## API Spec
|
111 |
|
112 | The `JwtService` uses [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) underneath.
|
113 |
|
114 | #### jwtService.sign(payload: string | Object | Buffer): string
|
115 | The sign method is an implementation of jsonwebtoken `.sign()`.
|
116 |
|
117 | #### jwtService.verify\<T extends object = any>(token: string): T
|
118 | The sign method is an implementation of jsonwebtoken `.verify()`.
|
119 |
|
120 | #### jwtService.decode(token: string, options: DecodeOptions): object | string
|
121 | The sign method is an implementation of jsonwebtoken `.decode()`.
|
122 |
|
123 | The `JwtModule` takes an `options` object:
|
124 | - `secretOrPrivateKey` [read more](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)
|
125 | - `signOptions` [read more](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)
|
126 | - `publicKey` PEM encoded public key for RSA and ECDSA
|
127 | - `verifyOptions` [read more](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)
|
128 |
|
129 | ## Support
|
130 |
|
131 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
132 |
|
133 | ## Stay in touch
|
134 |
|
135 | * Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
|
136 | * Website - [https://nestjs.com](https://nestjs.com/)
|
137 | * Twitter - [@nestframework](https://twitter.com/nestframework)
|
138 |
|
139 | ## License
|
140 |
|
141 | Nest is [MIT licensed](LICENSE).
|