UNPKG

5.91 kBMarkdownView Raw
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 <!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
25 [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
26
27## Description
28
29JWT 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
39Import `JwtModule`:
40
41```typescript
42@Module({
43 imports: [JwtModule.register({ secretOrPrivateKey: 'key' })],
44 providers: [...],
45})
46export class AuthModule {}
47```
48
49Inject `JwtService`:
50
51```typescript
52@Injectable()
53export class AuthService {
54 constructor(private readonly jwtService: JwtService) {}
55}
56```
57
58## Async options
59
60Quite 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
65JwtModule.registerAsync({
66 useFactory: () => ({
67 secretOrPrivateKey: 'key',
68 }),
69})
70```
71Obviously, our factory behaves like every other one (might be `async` and is able to inject dependencies through `inject`).
72
73```typescript
74JwtModule.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
85JwtModule.registerAsync({
86 useClass: JwtConfigService,
87})
88```
89Above construction will instantiate `JwtConfigService` inside `JwtModule` and will leverage it to create options object.
90```typescript
91class JwtConfigService implements JwtOptionsFactory {
92 createJwtOptions(): JwtModuleOptions {
93 return {
94 secretOrPrivateKey: 'key',
95 };
96 }
97}
98```
99
100**3. Use existing**
101```typescript
102JwtModule.registerAsync({
103 imports: [ConfigModule],
104 useExisting: ConfigService,
105}),
106```
107It 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
112The `JwtService` uses [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) underneath.
113
114#### jwtService.sign(payload: string | Object | Buffer): string
115The sign method is an implementation of jsonwebtoken `.sign()`.
116
117#### jwtService.verify\<T extends object = any>(token: string): T
118The sign method is an implementation of jsonwebtoken `.verify()`.
119
120#### jwtService.decode(token: string, options: DecodeOptions): object | string
121The sign method is an implementation of jsonwebtoken `.decode()`.
122
123The `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
131Nest 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
141Nest is [MIT licensed](LICENSE).