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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nest-auth-guard

v1.0.2

Published

A reusable NestJS Auth guard for validating JWT tokens locally or trusting API Gateway headers in production. This guard automatically switches behavior based on the `NODE_ENV` environment variable.

Readme

nest-auth-guard

A reusable NestJS Auth guard for validating JWT tokens locally or trusting API Gateway headers in production. This guard automatically switches behavior based on the NODE_ENV environment variable.


📦 Features

  • ✅ Auth JWT validation when running locally
  • ✅ API Gateway passthrough validation in production (via trusted headers)
  • ✅ Works with Bearer tokens
  • ✅ Lightweight and configurable
  • ✅ Built for microservice environments

🚀 Installation

1. Install the package

npm install nest-auth-guard

2. Install required peer dependencies

npm install @nestjs/common @nestjs/core @nestjs/passport passport passport-jwt ioredis

🔐 Required Environment Variables

| Key | Example Value | Required | Description | | ---------------- | ----------------------- | -------- | ------------------------------------- | --- | | NODE_ENV | local or production | ✅ | Used to switch between local and prod | | | REDIS_HOST | localhost | ✅ | Redis host (defaults to localhost) | | REDIS_PORT | 6379 | ✅ | Redis port (defaults to 6379) | | REDIS_PASSWORD | your-password | ❌ | Redis password (optional) | | REDIS_DB | 0 | ✅ | Redis database index (defaults to 0) | | REDIS_USERNAME | your-username | ❌ | Redis username (optional) |

Add these in your .env file:

NODE_ENV=local
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=your-password  # Optional
REDIS_DB=0  # Optional: database index
REDIS_USERNAME=your-username # Optional

🔁 In production, you can set NODE_ENV=production and pass user info via x-user header.


🧪 Usage in a NestJS Service

1. Enable .env loading

In app.module.ts of your NestJS service:

import { ConfigModule } from "@nestjs/config";

@Module({
  imports: [ConfigModule.forRoot({ isGlobal: true })],
})
export class AppModule {}

2. Protect a route using the guard

import { Controller, Get, Req, UseGuards } from "@nestjs/common";
import { AuthGuard } from "nest-auth0-guard";

@Controller("profile")
@UseGuards(AuthGuard)
export class ProfileController {
  @Get()
  getProfile(@Req() req) {
    return req.user; // decoded token or forwarded user info
  }
}

🛡 How It Works

| NODE_ENV | Behavior | | ------------ | ------------------------------------------ | | local | Validates JWT from Authorization: Bearer | | production | Reads user from x-user header |

Redis Integration

The guard automatically fetches additional user data from Redis using the user.userId field as the key. If Redis data is found, it merges with the authenticated user object. The Redis key format is user:{user.userId}.

Example Redis data structure:

{
  "user:68fb768c7f86363f4140899c": {
    "preferences": { "theme": "dark" },
    "profile": { "avatar": "https://example.com/avatar.jpg" },
    "settings": { "notifications": true }
  }
}