npm-oauth
v1.0.4
Published
Token authentication package with Redis cache and gRPC fallback for restaurant services
Downloads
20
Maintainers
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-oauthUsage
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
