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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sologence/nestjs-auth

v2.0.1

Published

## Overview

Downloads

20

Readme

NestJS Authentication Module

Overview

This NestJS Authentication Module provides a robust, flexible authentication system with JWT-based authentication, user management, and role-based access control.

Features

  • JWT-based authentication
  • Currently for SQL database
  • User registration (with multiple roles)
  • User activation by administrators
  • Password management (change and reset)
  • Super admin registration with strict controls
  • Secure password hashing
  • Role-based access control

Installation

Install the package using npm:

npm install @your-org/nestjs-auth-module

Configuration

Module Setup

import { AuthModule } from '@your-org/nestjs-auth-module';
import { CustomAuthConfigProvider } from './custom-auth-config.provider';

@Module({
  imports: [
    AuthModule.forExistingDataSource('default', {
      provide: 'AUTH_CONFIG_PROVIDER',
      useClass: CustomAuthConfigProvider
    })
  ]
})
export class AppModule {}

Creating a Config Provider

import { Injectable } from '@nestjs/common';
import { AuthConfigProvider } from '@your-org/nestjs-auth-module';

@Injectable()
export class CustomAuthConfigProvider implements AuthConfigProvider {
  getJwtConfig() {
    return {
      secret: process.env.JWT_SECRET,
      expiresIn: '1h',
      issuer: 'YourApp'
    };
  }
}

Usage Examples

User Registration

@Injectable()
export class UserService {
  constructor(private authService: AuthService) {}

  async createUser() {
    // Register a new user with a specific role
    const user = await this.authService.registerUser(
      '[email protected]', 
      'password123', 
      USER_ROLE.TECH_ROLE
    );
  }

  async createSuperAdmin() {
    // Register a super admin (only one allowed)
    const superAdmin = await this.authService.registerSuperAdmin(
      '[email protected]', 
      'securePassword'
    );
  }
}

User Authentication

@Injectable()
export class AuthController {
  constructor(private authService: AuthService) {}

  async login(email: string, password: string) {
    // Validate user credentials
    const result = await this.authService.validateUser(email, password);
    
    if (result.isValidated) {
      return {
        token: result.token,
        user: result.user
      };
    }
  }

  async validateToken(token: string) {
    // Validate an existing JWT token
    const validationResult = await this.authService.validateUserFromJWT(token);
    return validationResult.isValidated;
  }
}

User Activation

@Injectable()
export class AdminService {
  constructor(private authService: AuthService) {}

  async activateNewUser(userEmail: string) {
    // Activate a user (requires admin credentials)
    const activatedUser = await this.authService.activateUser(
      userEmail, 
      '[email protected]', 
      'adminPassword'
    );
  }
}

Password Management

@Injectable()
export class UserManagementService {
  constructor(private authService: AuthService) {}

  async changeUserPassword(token: string) {
    // Change user's password
    await this.authService.changePassword(
      token, 
      'oldPassword', 
      'newPassword'
    );
  }

  async resetUserPassword(adminToken: string, userEmail: string) {
    // Super admin can reset user password
    const resetResult = await this.authService.resetUserPassword(
      adminToken, 
      userEmail
    );
    
    if (resetResult.success) {
      // Send new password to user
      console.log(resetResult.newPassword);
    }
  }
}

User Roles

The module supports the following user roles:

  • SUPER_ADMIN: Full system access
  • ADMIN: Administrative privileges
  • TECH_ROLE: Standard user role
  • More roles can be added in the USER_ROLE enum

Security Features

  • Passwords are hashed using bcrypt
  • JWT tokens with configurable expiration
  • Role-based access control
  • Only one super admin can be registered
  • Users are inactive by default and require activation

Error Handling

The module throws specific exceptions:

  • UnauthorizedException
  • ConflictException
  • InternalServerErrorException
  • NotFoundException

Environment Variables

Recommended environment variables:

  • JWT_SECRET: Secret key for JWT token generation
  • JWT_EXPIRES_IN: Token expiration time

Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a new Pull Request

License

[Your License Here]

Support

For support, please open an issue in the GitHub repository or contact [[email protected]].