3.0.33 • Published 3 months ago

@alphaapps/nestjs-auth v3.0.33

Weekly downloads
133
License
MIT
Repository
gitlab
Last release
3 months ago

Authentication Module:

Installation:

npm install @alphaapps/nestjs-auth

This is the basic module for authentication, and it can be used as the following:

@Module({
  imports: [
    AuthModule.register({
      userModel: User,
      expiresIn: 0,
      oneSessionPerAccount: false,
      loginMethods: [{
        loginField: 'phoneNumber',
        passwordField: 'pin'
      }, {
        loginField: 'email',
        passwordField: 'password'
      }]
    })
  ]
})
export default class AppModule {}

Options passed to the register method:

  • userModel: typeof AuthUser: Indicates the main model if the user in the system. This model must extend the AuthUser (can be imported from @alphaapps/nestjs-auth) model in order for it to work.
  • expiresIn: number The age of the authentication token (in seconds). 0 means the token does NOT expire.
  • oneSessionPerAccount: boolean: When set to true will generate a new token on each successful login.
  • loginMethods: { loginField: string, passwordField: string }[]: The allowed login methods in the system.
  • property?: string: The name of the property for the user object to be saved in the request. default: 'user'
  • anonymousRole?: string: The name of the role in the system that can access specific resources anonymously. default: 'any'
  • userFindOptions?: FindOptions: The options sent to database when selecting the user. This is useful when we want to include any relation and has it with the user object in the request.
  • authModulePath?: string: The path of the authentication routes.
  • firebaseOTPValidation?: boolean: Indicates whether we use Firebase OTP service, or our own. default: false
  • useRoles?: boolean: Indicates whether we have roles in the system or not. default: true
  • rolesRelationName?: string: The name of the roles relation in the User model. default: 'associatedRoles'
  • sendOTPSMS?: (number: string, message: string, data: Record<string, any>) => Promise<void>: A function called when sending an SMS. This is useful when we want to customize the OTP message.

Included Models:

  • AuthUser: The main User model, the User model in the system must extend this model to add its custom properties and relations. It already has an association with Rols model.
  • Role: The model used to define roles and ACLs in the system.
  • VerifiactionCode: Used to save verification codes when validating a phone number.

A Note about roles:

Roles are dynamic in our applications, meaning they are saved in the database and retrieved on every startup of the app. In order to use it for pre-defined roles (like customer, client, agent... etc) an array of default roles is passed to the initiation of the RolesModule like this:

RoleModule.register([{
  role: 'default',
  resource: 'User',
  action: 'read:own'
}, {
  role: 'default',
  resource: 'User',
  action: 'update:own'
}])

Hint: RoleModule can be imported from @alphaapps/nestjs-auth

Auth Routes:

There's a bunch of routes that are generated when using this module that can be used out of the box.
All APIs are prefixed with the authModulePath specified in the AuthModule.register method.

  • validate-number:
    • Body:
      • phoneNumber: string
    • Response:
      • next: 'login' | 'register'
      • user: User
    • Headers:
      • Accept-Language
      • X-App-Version
      • X-Device-Platform
  • signup:
    Creates a new user in the application. It internally calls User.alphaCreate method which can be overridden in the User model inside the application.
    • Body:
      • name: string
      • phoneNumber?: string
      • pin?: string
      • email?: string
      • password?: string
      • additionalData?: Record<string, unknown> This is used to include any additional info in the sign-up process.
    • Headers:
      • Accept-Language
      • X-App-Version
      • X-Device-Platform
    • Response:
      • user: User
      • token: string
  • signin:
    • Body:
      • phoneNumber?: string
      • pin?: string
      • email?: string
      • password?: string
    • Response:
      • user: User
      • token: string
  • validate-otp:
    • Body:
      • phoneNumber: string
      • code: string
  • reset-pin:
    • Body:
      • phoneNumber: string
      • code: string
      • pin: string
    • Headers:
      • Accept-Language
      • X-App-Version
      • X-Device-Platform
    • Response:
      • user: User
      • token: string
  • resend-otp:
    • Body:
      • phoneNumber: string
    • Headers: - Accept-Language - X-App-Version - X-Device-Platform

      Hint: In order to override the behaviour of one (or more) of the routes a controller with the same path as authModulePath can be used.
      IMPORTANT: This module (that has the overridden routes) BEFORE the AuthModule in the AppModule imports

3.0.33

3 months ago

3.0.31

5 months ago

3.0.21

1 year ago

3.0.27

1 year ago

3.0.20

1 year ago

3.0.17

3 years ago

3.0.14

3 years ago

3.0.13

3 years ago

3.0.8

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

2.0.0

3 years ago

3.0.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.121-beta.0

3 years ago

1.2.122

3 years ago

1.2.121

3 years ago

1.2.114

3 years ago

1.2.106

3 years ago

1.2.105

3 years ago

1.2.104

3 years ago

1.2.94

4 years ago

1.2.93

4 years ago

1.2.92

4 years ago

1.2.81

4 years ago

1.2.80

4 years ago

1.2.79

4 years ago

1.2.78

4 years ago

1.2.77

4 years ago

1.2.75

4 years ago

1.2.72

4 years ago

1.2.68

4 years ago

1.2.69

4 years ago

1.2.70

4 years ago

1.2.67

4 years ago

1.2.66

4 years ago

1.2.65

4 years ago

1.2.64

4 years ago

1.2.63

4 years ago

1.2.62-beta.3

4 years ago

1.2.62

4 years ago

1.2.62-beta.2

4 years ago

1.2.62-beta.1

4 years ago

1.2.62-beta.0

4 years ago

1.2.62-alpha.7

4 years ago

1.2.62-alpha.8

4 years ago

1.2.62-alpha.6

4 years ago

1.2.62-alpha.5

4 years ago

1.2.62-alpha.4

4 years ago

1.2.62-alpha.3

4 years ago

1.2.62-alpha.2

4 years ago

1.2.62-alpha.1

4 years ago

1.2.62-alpha.0

4 years ago

1.2.46

4 years ago

1.2.45

4 years ago

1.2.43

4 years ago

1.2.43-alpha.0

4 years ago

1.2.35

4 years ago

1.2.25

4 years ago

1.2.26

4 years ago

1.2.16

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.2

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.39

4 years ago

1.0.40

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.30

4 years ago

1.0.26

4 years ago

1.0.22

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago