npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alphaapps/nestjs-auth

v3.0.33

Published

Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth i

Downloads

218

Readme

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