All Projects → nestjs → Jwt

nestjs / Jwt

Licence: mit
JWT utilities module based on the jsonwebtoken package 🔓

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Jwt

Nest Passport
Nest authentication example using passport strategies
Stars: ✭ 44 (-81.03%)
Mutual labels:  nestjs, nest, jwt
Nest Angular
NestJS, Angular 6, Server Side Rendering (Angular Universal), GraphQL, JWT (JSON Web Tokens) and Facebook/Twitter/Google Authentication, Mongoose, MongoDB, Webpack, TypeScript
Stars: ✭ 307 (+32.33%)
Mutual labels:  nestjs, nest, jwt
Nestjs Sequelize Jwt
Nest + Sequelize + jwt
Stars: ✭ 127 (-45.26%)
Mutual labels:  nest, jwt, jsonwebtoken
Nestjs Graphql
nest-js starter which implement graphql module
Stars: ✭ 111 (-52.16%)
Mutual labels:  nest, jwt, jsonwebtoken
Jwtxploiter
A tool to test security of json web token
Stars: ✭ 130 (-43.97%)
Mutual labels:  jwt, jsonwebtoken
Nest Event
Event handling with decorators for NestJS Framework
Stars: ✭ 128 (-44.83%)
Mutual labels:  nestjs, nest
Throttler
A rate limiting module for NestJS to work with Fastify, Express, GQL, Websockets, and RPC 🧭
Stars: ✭ 145 (-37.5%)
Mutual labels:  nestjs, nest
Serverless Core
Serverless Core module for Nest framework (node.js) 🦊
Stars: ✭ 154 (-33.62%)
Mutual labels:  nestjs, nest
Mern Skeleton
A MERN stack skeleton web application [Full-Stack React Projects]
Stars: ✭ 114 (-50.86%)
Mutual labels:  jwt, jsonwebtoken
Nest User Auth
A starter build for a back end which implements managing users with MongoDB, Mongoose, NestJS, Passport-JWT, and GraphQL.
Stars: ✭ 145 (-37.5%)
Mutual labels:  nestjs, nest
Config
Configuration module for Nest framework (node.js) 🍓
Stars: ✭ 161 (-30.6%)
Mutual labels:  nestjs, nest
Stator
Stator, your go-to template for the perfect stack. 😍🙏
Stars: ✭ 217 (-6.47%)
Mutual labels:  nestjs, nest
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (-18.97%)
Mutual labels:  nestjs, nest
Schedule
Schedule module for Nest framework (node.js) ⏰
Stars: ✭ 137 (-40.95%)
Mutual labels:  nestjs, nest
Nest Permissions Seed
A simple application demonstrating the basic usage of permissions with NestJS.
Stars: ✭ 121 (-47.84%)
Mutual labels:  nestjs, jwt
Fake Api Jwt Json Server
A Fake API with JWT Authentication using json-server and jsonwebtoken
Stars: ✭ 151 (-34.91%)
Mutual labels:  jwt, jsonwebtoken
Crud
NestJs CRUD for RESTful APIs
Stars: ✭ 2,709 (+1067.67%)
Mutual labels:  nestjs, nest
Notadd
A microservice development architecture based on nest.js. —— 基于 Nest.js 的微服务开发架构。
Stars: ✭ 2,556 (+1001.72%)
Mutual labels:  nestjs, nest
Nest
A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) 🚀
Stars: ✭ 42,981 (+18426.29%)
Mutual labels:  nestjs, nest
Passport
Passport module for Nest framework (node.js) 🔑
Stars: ✭ 211 (-9.05%)
Mutual labels:  nestjs, nest

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Travis Linux Coverage Discord Backers on Open Collective Sponsors on Open Collective

Description

JWT utilities module for Nest based on the jsonwebtoken package.

Installation

$ npm i --save @nestjs/jwt

Usage

Import JwtModule:

@Module({
  imports: [JwtModule.register({ secret: 'hard!to-guess_secret' })],
  providers: [...],
})
export class AuthModule {}

Inject JwtService:

@Injectable()
export class AuthService {
  constructor(private readonly jwtService: JwtService) {}
}

Secret / Encryption Key options

If you want to control secret and key management dynamically you can use the secretOrKeyProvider function for that purpose.

JwtModule.register({
   /* Secret has precedence over keys */
  secret: 'hard!to-guess_secret',

  /* public key used in asymmetric algorithms (required if non other secrets present) */
  publicKey: '...',

  /* private key used in asymmetric algorithms (required if non other secrets present) */
  privateKey: '...'

  /* Dynamic key provider has precedence over static secret or pub/private keys */
  secretOrKeyProvider: (
    requestType: JwtSecretRequestType,
    tokenOrPayload: string | Object | Buffer,
    verifyOrSignOrOptions?: jwt.VerifyOptions | jwt.SignOptions
  ) => {
    switch (requestType) {
      case JwtSecretRequestType.SIGN:
        // retrieve signing key dynamically
        return 'privateKey';
      case JwtSecretRequestType.VERIFY:
        // retrieve public key for verification dynamically
        return 'publicKey';
      default:
        // retrieve secret dynamically
        return 'hard!to-guess_secret';
    }
  },
});

Async options

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.

1. Use factory

JwtModule.registerAsync({
  useFactory: () => ({
    secret: 'hard!to-guess_secret'
  })
});

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

JwtModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    secret: configService.get<string>('SECRET'),
  }),
  inject: [ConfigService],
}),

2. Use class

JwtModule.registerAsync({
  useClass: JwtConfigService
});

Above construction will instantiate JwtConfigService inside JwtModule and will leverage it to create options object.

class JwtConfigService implements JwtOptionsFactory {
  createJwtOptions(): JwtModuleOptions {
    return {
      secret: 'hard!to-guess_secret'
    };
  }
}

3. Use existing

JwtModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

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.

API Spec

The JwtService uses jsonwebtoken underneath.

jwtService.sign(payload: string | Object | Buffer, options?: JwtSignOptions): string

The 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.

jwtService.signAsync(payload: string | Object | Buffer, options?: JwtSignOptions): Promise<string>

The asynchronous .sign() method.

jwtService.verify<T extends object = any>(token: string, options?: JwtVerifyOptions): T

The 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.

jwtService.verifyAsync<T extends object = any>(token: string, options?: JwtVerifyOptions): Promise<T>

The asynchronous .verify() method.

jwtService.decode(token: string, options: DecodeOptions): object | string

The decode method is an implementation of jsonwebtoken .decode().

The JwtModule takes an options object:

  • secret is either a string, buffer, or object containing the secret for HMAC algorithms
  • secretOrKeyProvider function with the following signature (requestType, tokenOrPayload, options?) => jwt.Secret (allows generating either secrets or keys dynamically)
  • signOptions read more
  • privateKey PEM encoded private key for RSA and ECDSA with passphrase an object { key, passphrase } read more
  • publicKey PEM encoded public key for RSA and ECDSA
  • verifyOptions read more
  • secretOrPrivateKey (DEPRECATED!) read more

Support

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.

Stay in touch

License

Nest is MIT licensed.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].