3.0.0 • Published 9 months ago

@trxn/nestjs-authentication v3.0.0

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

Authentication Module

The nestjs-authentication module is that will handle your authentication. It will by give you access to some standard guard to use jwt and login based strategy authentication. Under the hood this module use jwt and passport to authenticate the connections.

Installation

You can install the nestjs-authentication module using npm:

npm install --save @trxn/nestjs-authentication

Getting Started

In your workspace create you own AuthenticationModule that will configure the AuthenticationModule.

import { Module } from '@nestjs/common';

import { AuthenticationModule } from '@trxn/nestjs-authentication';
import { UserModule } from './user.module';

@Module({
  imports: [AuthenticationModule.register({
    imports: [UserModule]
  })],
})
export class AppModule {}

The UserModule here is the UserModule provided by @trxn/nestjs-user and that expose a UserService. You can use your own UserService class by providing it directly in the options:

import { Module } from '@nestjs/common';

import { AuthenticationModule } from '@trxn/nestjs-authentication';
import { UserModule } from './user.module';

@Module({
  imports: [AuthenticationModule.register({
    UserService: MyUserServiceClass
  })],
})
export class AppModule {}

The UserService property respect the nestjs Provider type.

Configuration

After the first configuration your routes are not yet protected. Authentication Module give access to you to some guard class that you can use across your project. One of the best method to authenticate your application is to force the authentication to all the routes of your application and whitelist only the route that you need to be public. To do so AuthenticationModule give you access a guard and a decorator @Public to mark the route as public.

Here an example to authenticate all your routes:

import {
  AuthenticationModule,
  JwtGlobalAuthGuard,
} from '@trxn/nestjs-authentication';

@Module({
  imports: [
    AuthenticationModule.register(
      //... provide a UserService class
    ),
  ],
  controllers: [],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtGlobalAuthGuard,
    },
  ],
})
export class AppModule {}

CustomUserSelect

The AuthenticationModule is responsible to hydrate you user when a request hit your api. Some time you don't want only the user but you want to join some data attached to it. This module let you configured a object that will be used in junction with the `UserService provided to select the information to use to fetch the user.

@Module({
  imports: [
    AuthenticationModule.register(
      //... the UserService configuration

      customSelect: {
        id: true,
        roles: true,
        profile: {
          name: true,
          address: true,
          // ...
        }
      }
    ),
  ],
  controllers: [],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtGlobalAuthGuard,
    },
  ],
})
export cla

## Cookie authentication

You need if you want to use the authentication from cookie to add the cookie
parser package to your app module. You can look at the
[nestjs documentation](https://docs.nestjs.com/techniques/cookies#cookies).
**You must provide one or more secret to the cookie-parser package**,
`nestjs-authentication` use only signedCookie in production mode.

```typescript
import * as cookieParser from 'cookie-parser';
// somewhere in your initialization file
app.use(cookieParser('myScret'));

Encryption

By default AuthenticationModule use Bcrypt to parse and read the password. If you need to use something else you can provide to the AuthenticationModule your own EncryptionService that need to implement a compare and a hash method.

@Module({
  imports: [
    AuthenticationModule.register(
      //... provide a UserService class
      EncryptionService: EncryptionService
    ),
  ],
  controllers: [],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtGlobalAuthGuard,
    },
  ],
})
export class AppModule {}

You can look at the BcryptModule to found some example.

Bcrypt online generator

To be able to test you can use these bcrypt online generator to crypt your passwords.

2.2.7

10 months ago

3.0.0

9 months ago

2.2.5

11 months ago

2.2.1

11 months ago

2.2.0

12 months ago

2.2.3

11 months ago

2.2.2

11 months ago

2.2.4

11 months ago

2.1.22-next.5

12 months ago

2.1.22-next.3

12 months ago

2.1.22-next.4

12 months ago

2.1.22-next.1

12 months ago

2.1.22-next.2

12 months ago

2.1.22-next.0

12 months ago

2.1.9

1 year ago

2.1.12

1 year ago

2.1.10

1 year ago

2.1.11

1 year ago

2.1.4

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

2.1.0-next.1

1 year ago

2.1.0-next.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.3

1 year ago

2.1.0

1 year ago

2.0.11-next.1

1 year ago

2.0.11-next.0

1 year ago

2.0.11-next.3

1 year ago

2.0.11-next.2

1 year ago

2.0.13

1 year ago

2.0.11

1 year ago

2.0.12

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.0-next.2

1 year ago

2.0.0-next.0

1 year ago

2.0.0-next.1

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.65.13-next.4

1 year ago

1.65.13-next.5

1 year ago

1.65.11-beta.3

1 year ago

1.65.3

1 year ago

1.65.13-next.2

1 year ago

1.65.4

1 year ago

1.65.5

1 year ago

1.65.13-next.8

1 year ago

1.65.6

1 year ago

1.65.11-next.0

1 year ago

1.65.7

1 year ago

1.65.13-next.6

1 year ago

1.65.8

1 year ago

1.65.13-next.7

1 year ago

1.65.9

1 year ago

1.65.10

1 year ago

1.65.12

1 year ago

1.65.11

1 year ago

1.65.13-next.0

1 year ago

1.65.11-beta.0

1 year ago

1.65.13-next.1

1 year ago

1.65.11-beta.2

1 year ago

1.65.11-beta.1

1 year ago

1.65.2

1 year ago