6.0.5 • Published 4 months ago

@txdmobile/nestjs-auth v6.0.5

Weekly downloads
2
License
MIT
Repository
-
Last release
4 months ago

TxD Mobile NestJS Auth

Nestjs library for auth using private and public key with user roles

Installation

$ npm install --save @nestjs/passport passport
$ npm install --save @txdmobile/nestjs-auth

Import Module

import { NestjsAuthModule } from '@txdmobile/nestjs-auth';

const config = {
  publicKey: "<PUBLIC_KEY>"
}

@Module({
  imports: [NestjsAuthModule.config(config)],
  ...

Note: NestjsAuthModule was defined with the decorator "@Global", therefore it should only be imported in the root module of the app

Use Bearer Authentication

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { RolesGuard, Roles } from '@txdmobile/nestjs-auth';
import { AuthGuard } from '@nestjs/passport';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @UseGuards(AuthGuard())
  getHello(): string {
    return this.appService.getHello();
  }
}

Use Bearer Authentication with Roles

import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { RolesGuard, Roles } from '@txdmobile/nestjs-auth';
import { AuthGuard } from '@nestjs/passport';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @Roles('admin')
  @UseGuards(AuthGuard(), RolesGuard)
  getHello(): string {
    return this.appService.getHello();
  }
}

Retrieve the user object from request

  ...
  @Get('user')
  @UseGuards(AuthGuard())
  getUser(@Req() req) {
    return req.user;
  }

  //this code returns the following json
  {
    "uid": "<username>",
    "roles": [
        "admin"
    ]
  }
6.0.5

4 months ago

6.0.1

4 months ago

6.0.0

4 months ago

6.0.3

4 months ago

6.0.2

4 months ago

6.0.4

4 months ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.0.0

3 years ago

3.0.0

3 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago