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

techxnix-auth

v1.0.17

Published

Authentication library for Techxnix applications

Readme

Techxnix Auth Library

A reusable authentication library for NestJS applications.

Table of Contents

Installation

npm install techxnix-auth

Setup

  1. Import and configure TechxnixAuthModule in your app.module.ts:
import { Module } from '@nestjs/common';
import { TechxnixAuthModule } from 'techxnix-auth';

@Module({
    imports: [
        TechxnixAuthModule.forRoot({
            jwt: {
                secret: 'your-secret-key',
                refreshSecret: 'your-refresh-secret-key',
                expiresIn: '15m',
                refreshExpiresIn: '7d',
            },
        }),
    ],
})
export class AppModule {}

Decorators

@Public()

Marks a route as public, bypassing authentication:

@Public()
@Get('public-route')
publicRoute() {
    return 'This route is public';
}

@GetUser()

Retrieves the authenticated user from the request:

@Get('profile')
getProfile(@GetUser() user: IAuthPayload) {
    return user;
}

Guards

JwtAuthGuard

Protects routes requiring authentication:

@UseGuards(JwtAuthGuard)
@Get('protected')
protectedRoute(@GetUser() user: IAuthPayload) {
    return `Hello ${user.email}`;
}

JwtRefreshAuthGuard

Protects the refresh token endpoint:

@UseGuards(JwtRefreshAuthGuard)
@Post('refresh')
async refreshToken(@GetUser() user: IAuthPayload) {
    return this.tokenService.generateTokens(user);
}

Services

TokenService

Handles JWT token generation and verification:

constructor(private tokenService: TokenService) {}

async generateTokens(user: IAuthPayload) {
    return this.tokenService.generateTokens(user);
}

async verifyToken(token: string) {
    return this.tokenService.verifyToken(token);
}

async verifyRefreshToken(token: string) {
    return this.tokenService.verifyRefreshToken(token);
}

Complete Example

Here's a complete example of an auth controller:

import { Controller, Post, Get, Body, UseGuards } from '@nestjs/common';
import { TokenService } from 'techxnix-auth';
import { Public, GetUser, JwtAuthGuard, JwtRefreshAuthGuard } from 'techxnix-auth';
import { IAuthPayload } from 'techxnix-auth';

@Controller('auth')
export class AuthController {
    constructor(
        private readonly tokenService: TokenService,
        private readonly userService: UserService
    ) {}

    @Public()
    @Post('signin')
    async signIn(@Body() credentials: { email: string; password: string }) {
        const user = await this.userService.validateUser(
            credentials.email,
            credentials.password
        );

        return this.tokenService.generateTokens({
            sub: user.id,
            email: user.email,
            firstName: user.firstName,
            lastName: user.lastName
        });
    }

    @UseGuards(JwtAuthGuard)
    @Get('me')
    getProfile(@GetUser() user: IAuthPayload) {
        return user;
    }

    @UseGuards(JwtRefreshAuthGuard)
    @Post('refresh')
    async refreshToken(@GetUser() user: IAuthPayload) {
        return this.tokenService.generateTokens(user);
    }
}

Configure the module in your app.module.ts:

import { Module } from '@nestjs/common';
import { TechxnixAuthModule } from 'techxnix-auth';

@Module({
    imports: [
        TechxnixAuthModule.forRoot({
            jwt: {
                secret: 'your-secret-key',
                refreshSecret: 'your-refresh-secret-key',
                expiresIn: '15m',
                refreshExpiresIn: '7d',
            },
        }),
    ],
})
export class AppModule {}

Testing

Mock Auth Controller

For testing purposes, you can use the provided mock auth controller. Here's an example of how to set it up:

import { Controller, Post, Get, Body, UseGuards } from '@nestjs/common';
import { TokenService } from 'techxnix-auth';
import { Public, GetUser, JwtAuthGuard, JwtRefreshAuthGuard } from 'techxnix-auth';
import { IAuthPayload } from 'techxnix-auth';

// Mock user data for testing
const mockAuthPayload: IAuthPayload = {
    sub: 'sub-uuid',
    email: '[email protected]',
    firstName: 'Test',
    lastName: 'User',
};

@Controller('auth')
export class MockAuthController {
    constructor(private readonly tokenService: TokenService) {}

    @Public()
    @Post('signin')
    async signIn(@Body() payload: { email: string; password: string }) {
        // Simple mock authentication
        if (payload.email !== mockAuthPayload.email || payload.password !== 'password') {
            throw new Error('Invalid credentials');
        }

        return this.tokenService.generateTokens({
            ...mockAuthPayload,
            email: payload.email,
        });
    }

    @UseGuards(JwtAuthGuard)
    @Get('me')
    getProfile(@GetUser() user: IAuthPayload) {
        return user;
    }

    @UseGuards(JwtRefreshAuthGuard)
    @Post('refresh')
    async refreshToken(@GetUser() user: IAuthPayload) {
        return this.tokenService.generateTokens({
            ...mockAuthPayload,
            email: user.email,
        });
    }
}

This mock controller provides three endpoints:

  • POST /auth/signin: Mock authentication endpoint that accepts email/password
  • GET /auth/me: Protected endpoint that returns the current user's profile
  • POST /auth/refresh: Protected endpoint that refreshes the access token using a refresh token

Running E2E Tests

To run the e2e tests:

npm run test:e2e

The e2e tests verify the following authentication flows:

  • Sign in and token generation
  • Protected route access with valid token
  • Protected route access with invalid token
  • Token refresh with valid refresh token
  • Token refresh with invalid token

Make sure to configure your jest-e2e.json properly:

{
    "moduleFileExtensions": ["js", "json", "ts"],
    "rootDir": "..",
    "testEnvironment": "node",
    "testRegex": ".e2e-spec.ts$",
    "transform": {
        "^.+\\.(t|j)s$": ["ts-jest", {
            "tsconfig": "<rootDir>/tsconfig.json"
        }]
    },
    "moduleNameMapper": {
        "^@/(.*)$": "<rootDir>/src/$1"
    }
}