57 lines
1.8 KiB
Markdown
57 lines
1.8 KiB
Markdown
# Jwt Generator
|
|
|
|
A NestJs Library to generate JWTs.
|
|
|
|
## Install
|
|
|
|
```bash
|
|
npm i --save @apihub24/jwt-generator
|
|
```
|
|
|
|
## Requirements
|
|
|
|
| Dependency | Description |
|
|
| ------------------------- | ---------------------------------------------------------- |
|
|
| @nestjs/config | use the NestJs Config Module to get some keys listed below |
|
|
| @apihub24/session_service | used to create new Sessions when a new Token was created |
|
|
| JWT_SECRET | NestJs Config Key for Secret used to Sign JWT |
|
|
| JWT_ISSUER | NestJs Config Key for the JWT Issuer |
|
|
| JWT_AUDIENCE | NestJs Config Key for the JWT Audience |
|
|
|
|
## Usage
|
|
|
|
```typescript
|
|
import {
|
|
JwtGeneratorModule,
|
|
JwtService,
|
|
APIHUB24_TOKEN_SERVICE_INJECTION_KEY,
|
|
} from "@apihub24/jwt-generator";
|
|
// we use the @apihub24/session_service implementation from the InMemorySessionsModule
|
|
import { InMemorySessionsModule } from "@apihub24/in-memory-sessions";
|
|
// use the takeExportedProviders function from NestJs Helper Module to select the exported Providers
|
|
import { takeExportedProviders } from "@apihub24/nestjs-helper";
|
|
|
|
// use JwtGeneratorModule
|
|
const inMemorySessionsModule = InMemorySessionsModule.forRoot();
|
|
const jwtGeneratorModule = JwtGeneratorModule.forRoot([
|
|
...takeExportedProviders(inMemorySessionsModule),
|
|
]);
|
|
|
|
@Module({
|
|
imports: [jwtGeneratorModule],
|
|
})
|
|
|
|
// now you can inject the service with injection Token
|
|
import * as tokenAuthentication from "@apihub24/token-authentication";
|
|
|
|
export class Test {
|
|
constructor(
|
|
@Inject(APIHUB24_TOKEN_SERVICE_INJECTION_KEY) jwtGenerator: JwtService
|
|
);
|
|
|
|
async test(session: tokenAuthentication.ISession) {
|
|
const token = await this.jwtGenerator.generate(session);
|
|
}
|
|
}
|
|
```
|