3.0.0 • Published 4 years ago

@nestjs-mods/jwt v3.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

An JWT based authentication module for a nestjs application.

Configuration

import { getTokenByBearerHeader, getTokenByCookie, JwtModule, JwtGuard } from '@nestjs-mods/jwt';
import { APP_GUARD } from '@nestjs/core';

@Module({
  imports: [
    JwtModule.forRoot({
      secret: 'super-secret-phrase',
      defaultSignOptions: {
        expiresIn: '7 days',
        algorithm: 'HS512',
      },
      tokenResolver: [
        getTokenByCookie('user'),         // first try to extract a token from the cookie
        getTokenByBearerHeader,           // then try to extract the token by a bearer header
      ],
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    { provide: APP_GUARD, useClass: JwtGuard },
  ],
})
export class AppModule { }

Resolve a token from request

You can add as much token resolver as you like the first one in the list that returns a token wins.

getTokenByCookie(cookieName: string)

If you store your token in a cookie you need the cookie-parser middleware.

getTokenByBearerHeader()

Resolves the token from the authorization header

API

Param decorators

import { Payload, Token } from '@nestjs-mods/jwt';

// ...

@Get()
route(@Payload() payload: any, @Token() token: string) {

// ...

}

TokenService

Because the AuthModule is a global scoped module you don't have to import the AuthModule to use the TokenService

import { TokenService } from '@nestjs-mods/jwt';
import { Controller, Post } from '@nestjs/common';

@Controller('user')
export class UserController {

  constructor(
    private tokenService: TokenService<{ customPayloadField: string; }>,
  ) { }

  @Post('login')
  async login() {

    const token = await this.tokenService.createToken({
      customPayloadField: 'Hello world',
    });

  }
}