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

npm-oauth

v1.0.4

Published

Token authentication package with Redis cache and gRPC fallback for restaurant services

Downloads

20

Readme

npm-oauth

Token authentication package with Redis cache and gRPC fallback for restaurant services.

Features

  • Redis Cache First: Checks Redis cache for token validation data first
  • gRPC Fallback: Falls back to gRPC service when cache misses
  • Auto-caching: Automatically caches successful gRPC responses
  • Flexible Configuration: Support for Redis (host, port, password, db) and gRPC configurations
  • Built-in Logging: Optional logging with configurable levels
  • TypeScript Support: Full TypeScript definitions included

Installation

npm install npm-oauth

Usage

Basic Usage

import { authenticateToken } from 'npm-oauth';

const config = {
  redis: {
    host: 'localhost',
    port: 6379,
    password: 'your-redis-password', // optional
    db: 0 // optional, defaults to 0
  },
  grpc: {
    host: 'localhost',
    port: 50051
  },
  logging: { // optional
    enabled: true,
    level: 'info' // 'error' | 'warn' | 'info' | 'debug'
  }
};

const result = await authenticateToken('your-token-here', config);

if (result.success) {
  console.log('User data:', result.data);
  console.log('From cache:', result.fromCache);
} else {
  console.error('Authentication failed:', result.message);
}

Using Global Service Instance

For better performance when making multiple requests:

import { 
  initializeGlobalAuthService, 
  authenticateTokenWithGlobalService,
  disconnectGlobalService 
} from 'npm-oauth';

// Initialize once
await initializeGlobalAuthService(config);

// Use multiple times
const result1 = await authenticateTokenWithGlobalService('token1');
const result2 = await authenticateTokenWithGlobalService('token2');

// Cleanup when done
await disconnectGlobalService();

Manual Service Management

import { createAuthService } from 'npm-oauth';

const authService = createAuthService(config);

const result = await authService.authenticateToken('your-token');

// Don't forget to cleanup
await authService.disconnect();

Configuration

AuthConfig Interface

interface AuthConfig {
  redis: {
    host: string;
    port: number;
    password?: string;
    db?: number;
  };
  grpc: {
    host: string;
    port: number;
  };
  logging?: {
    enabled?: boolean;
    level?: 'error' | 'warn' | 'info' | 'debug';
  };
}

AuthResult Interface

interface AuthResult {
  success: boolean;
  data?: {
    user_id: number;
    username: string;
    platform: number;
    user_role: string;
    restaurant_id: number;
    restaurant_brand_id: number;
    branch_id: number;
  };
  message?: string;
  fromCache?: boolean; // indicates if result came from Redis cache
}

Cache Key Pattern

The package uses the following Redis key pattern:

cache_authenticate:restaurant:access_token:{token}

Cache TTL is set to 3600 seconds (1 hour) by default.

Error Handling

The package handles various error scenarios:

  • Redis connection failures (falls back to gRPC)
  • gRPC service unavailability
  • Invalid token formats
  • Network timeouts
  • Parsing errors

All errors are logged according to the configured logging level.

Requirements

  • Node.js >= 16.0.0
  • Redis server
  • gRPC service implementing ValidateTokenService

License

MIT