{"id":14111175,"url":"https://github.com/nestjs/jwt","last_synced_at":"2025-05-13T19:15:11.022Z","repository":{"id":37251613,"uuid":"138471435","full_name":"nestjs/jwt","owner":"nestjs","description":"JWT utilities module based on the jsonwebtoken package 🔓","archived":false,"fork":false,"pushed_at":"2025-05-12T22:43:41.000Z","size":7114,"stargazers_count":634,"open_issues_count":2,"forks_count":85,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-12T23:32:45.174Z","etag":null,"topics":["javascript","jsonwebtoken","jwt","nest","nestjs","nodejs","typescript"],"latest_commit_sha":null,"homepage":"https://nestjs.com","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nestjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-06-24T10:04:11.000Z","updated_at":"2025-05-12T22:43:44.000Z","dependencies_parsed_at":"2023-10-15T18:55:59.878Z","dependency_job_id":"8800a4a4-8365-41c6-ab39-c003e1554d4a","html_url":"https://github.com/nestjs/jwt","commit_stats":{"total_commits":1725,"total_committers":27,"mean_commits":"63.888888888888886","dds":"0.47710144927536235","last_synced_commit":"e259af6f7616d48e9a630857414570b6e6c9473f"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjs%2Fjwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjs%2Fjwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjs%2Fjwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nestjs%2Fjwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nestjs","download_url":"https://codeload.github.com/nestjs/jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843168,"owners_count":21972867,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","jsonwebtoken","jwt","nest","nestjs","nodejs","typescript"],"created_at":"2024-08-14T10:03:10.774Z","updated_at":"2025-05-13T19:15:10.992Z","avatar_url":"https://github.com/nestjs.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" width=\"120\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master\n[travis-url]: https://travis-ci.org/nestjs/nest\n[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux\n[linux-url]: https://travis-ci.org/nestjs/nest\n\n  \u003cp align=\"center\"\u003eA progressive \u003ca href=\"http://nodejs.org\" target=\"blank\"\u003eNode.js\u003c/a\u003e framework for building efficient and scalable server-side applications.\u003c/p\u003e\n    \u003cp align=\"center\"\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@nestjs/core.svg\" alt=\"NPM Version\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@nestjs/core.svg\" alt=\"Package License\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/~nestjscore\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@nestjs/core.svg\" alt=\"NPM Downloads\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://discord.gg/G7Qnnhy\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/discord-online-brightgreen.svg\" alt=\"Discord\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#backer\"\u003e\u003cimg src=\"https://opencollective.com/nest/backers/badge.svg\" alt=\"Backers on Open Collective\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/nest#sponsor\"\u003e\u003cimg src=\"https://opencollective.com/nest/sponsors/badge.svg\" alt=\"Sponsors on Open Collective\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://paypal.me/kamilmysliwiec\"\u003e\u003cimg src=\"https://img.shields.io/badge/Donate-PayPal-dc3d53.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://twitter.com/nestframework\"\u003e\u003cimg src=\"https://img.shields.io/twitter/follow/nestframework.svg?style=social\u0026label=Follow\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n  \u003c!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)\n  [![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)--\u003e\n\n## Description\n\nJWT utilities module for [Nest](https://github.com/nestjs/nest) based on the [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) package.\n\n## Installation\n\n```bash\n$ npm i --save @nestjs/jwt\n```\n\n## Usage\n\nImport `JwtModule`:\n\n```typescript\n@Module({\n  imports: [JwtModule.register({ secret: 'hard!to-guess_secret' })],\n  providers: [...],\n})\nexport class AuthModule {}\n```\n\nInject `JwtService`:\n\n```typescript\n@Injectable()\nexport class AuthService {\n  constructor(private readonly jwtService: JwtService) {}\n}\n```\n\n## Secret / Encryption Key options\n\nIf you want to control secret and key management dynamically you can use the `secretOrKeyProvider` function for that purpose. You also can use asynchronous version of `secretOrKeyProvider`.\nNOTE: For asynchronous version of `secretOrKeyProvider`, synchronous versions of `.sign()` and `.verify()` will throw an exception.\n\n```typescript\nJwtModule.register({\n   /* Secret has precedence over keys */\n  secret: 'hard!to-guess_secret',\n\n  /* public key used in asymmetric algorithms (required if non other secrets present) */\n  publicKey: '...',\n\n  /* private key used in asymmetric algorithms (required if non other secrets present) */\n  privateKey: '...',\n\n  /* Dynamic key provider has precedence over static secret or pub/private keys */\n  secretOrKeyProvider: (\n    requestType: JwtSecretRequestType,\n    tokenOrPayload: string | Object | Buffer,\n    verifyOrSignOrOptions?: jwt.VerifyOptions | jwt.SignOptions\n  ) =\u003e {\n    switch (requestType) {\n      case JwtSecretRequestType.SIGN:\n        // retrieve signing key dynamically\n        return 'privateKey';\n      case JwtSecretRequestType.VERIFY:\n        // retrieve public key for verification dynamically\n        return 'publicKey';\n      default:\n        // retrieve secret dynamically\n        return 'hard!to-guess_secret';\n    }\n  },\n});\n```\n\n## Async options\n\nQuite 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.\n\n**1. Use factory**\n\n```typescript\nJwtModule.registerAsync({\n  useFactory: () =\u003e ({\n    secret: 'hard!to-guess_secret'\n  })\n});\n```\n\nObviously, our factory behaves like every other one (might be `async` and is able to inject dependencies through `inject`).\n\n```typescript\nJwtModule.registerAsync({\n  imports: [ConfigModule],\n  useFactory: async (configService: ConfigService) =\u003e ({\n    secret: configService.get\u003cstring\u003e('SECRET'),\n  }),\n  inject: [ConfigService],\n}),\n```\n\n**2. Use class**\n\n```typescript\nJwtModule.registerAsync({\n  useClass: JwtConfigService\n});\n```\n\nAbove construction will instantiate `JwtConfigService` inside `JwtModule` and will leverage it to create options object.\n\n```typescript\nclass JwtConfigService implements JwtOptionsFactory {\n  createJwtOptions(): JwtModuleOptions {\n    return {\n      secret: 'hard!to-guess_secret'\n    };\n  }\n}\n```\n\n**3. Use existing**\n\n```typescript\nJwtModule.registerAsync({\n  imports: [ConfigModule],\n  useExisting: ConfigService,\n}),\n```\n\nIt 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.\n\n## API Spec\n\nThe `JwtService` uses [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) underneath.\n\n#### jwtService.sign(payload: string | Object | Buffer, options?: JwtSignOptions): string\n\nThe sign method is an implementation of jsonwebtoken `.sign()`. Differing from jsonwebtoken it also allows an additional `secret`, `privateKey`, and `publicKey` properties on `options` to override options passed in from the module. It only overrides the `secret`, `publicKey` or `privateKey` though not a `secretOrKeyProvider`.\nNOTE: Will throw an exception for asynchronous version of `secretOrKeyProvider`;\n\n#### jwtService.signAsync(payload: string | Object | Buffer, options?: JwtSignOptions): Promise\\\u003cstring\\\u003e\n\nThe asynchronous `.sign()` method.\n\n#### jwtService.verify\\\u003cT extends object = any\u003e(token: string, options?: JwtVerifyOptions): T\n\nThe verify method is an implementation of jsonwebtoken `.verify()`. Differing from jsonwebtoken it also allows an additional `secret`, `privateKey`, and `publicKey` properties on `options` to override options passed in from the module. It only overrides the `secret`, `publicKey` or `privateKey` though not a `secretOrKeyProvider`.\nNOTE: Will throw an exception for asynchronous version of `secretOrKeyProvider`;\n\n#### jwtService.verifyAsync\\\u003cT extends object = any\u003e(token: string, options?: JwtVerifyOptions): Promise\\\u003cT\\\u003e\n\nThe asynchronous `.verify()` method.\n\n#### jwtService.decode(token: string, options: DecodeOptions): object | string\n\nThe decode method is an implementation of jsonwebtoken `.decode()`.\n\nThe `JwtModule` takes an `options` object:\n\n- `secret` is either a string, buffer, or object containing the secret for HMAC algorithms\n- `secretOrKeyProvider` function with the following signature `(requestType, tokenOrPayload, options?) =\u003e jwt.Secret | Promise\u003cjwt.Secret\u003e` (allows generating either secrets or keys dynamically)\n- `signOptions` [read more](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)\n- `privateKey` PEM encoded private key for RSA and ECDSA with passphrase an object `{ key, passphrase }` [read more](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)\n- `publicKey` PEM encoded public key for RSA and ECDSA\n- `verifyOptions` [read more](https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback)\n- `secretOrPrivateKey` (DEPRECATED!) [read more](https://github.com/auth0/node-jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback)\n\n## Support\n\nNest 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).\n\n## Stay in touch\n\n- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)\n- Website - [https://nestjs.com](https://nestjs.com/)\n- Twitter - [@nestframework](https://twitter.com/nestframework)\n\n## License\n\nNest is [MIT licensed](LICENSE).\n","funding_links":["https://opencollective.com/nest","https://paypal.me/kamilmysliwiec"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjs%2Fjwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnestjs%2Fjwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnestjs%2Fjwt/lists"}