Skip to content

FinCube-23/validate-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@mskits/validate-auth

npm version License: ISC

A lightweight, TypeScript-ready authentication validation utility for NestJS microservices architecture.

✨ Features

  • 🚀 Simple & Fast - Minimal setup with maximum efficiency
  • 🔒 Secure - Built-in authorization header validation
  • 🏗️ Microservices Ready - Designed for NestJS microservices
  • 📦 TypeScript Support - Full type definitions included
  • RxJS Integration - Reactive programming support
  • 🛠️ Configurable - Flexible options support

📦 Installation

npm install @mskits/validate-auth
yarn add @mskits/validate-auth
pnpm add @mskits/validate-auth

🚀 Quick Start

import { validateAuth } from '@mskits/validate-auth';
import { ClientProxy } from '@nestjs/microservices';

// In your controller or service
async function protectedRoute(req: any, authClient: ClientProxy) {
  try {
    const user = await validateAuth(req, authClient);
    // User is authenticated, proceed with your logic
    return user;
  } catch (error) {
    // Handle authentication error
    throw new UnauthorizedException('Invalid authentication');
  }
}

📖 API Reference

validateAuth(req, client, options?)

Validates the authorization header from a request using a NestJS microservice client.

Parameters

Parameter Type Required Description
req any The request object containing headers
client ClientProxy NestJS microservice client for auth validation
options any Optional configuration object

Returns

  • Promise - Resolves with user data on successful validation
  • Throws - Error on validation failure or missing requirements

Example with Options

const user = await validateAuth(req, authClient, {
  skipExpiry: false,
  requiredRoles: ['admin', 'user']
});

🔧 Usage Examples

Basic Guard Implementation

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { validateAuth } from '@mskits/validate-auth';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private readonly authClient: ClientProxy) {}

  async canActivate(context: ExecutionContext): Promise<boolean> {
    const request = context.switchToHttp().getRequest();

    try {
      const user = await validateAuth(request, this.authClient);
      request.user = user; // Attach user to request
      return true;
    } catch {
      return false;
    }
  }
}

Controller Usage

import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { validateAuth } from '@mskits/validate-auth';

@Controller('protected')
export class ProtectedController {
  constructor(private readonly authClient: ClientProxy) {}

  @Get('profile')
  async getProfile(@Req() req: any) {
    const user = await validateAuth(req, this.authClient);
    return { message: 'Access granted', user };
  }
}

🏗️ Microservice Setup

Your authentication microservice should handle the validate-authorization message pattern:

@MessagePattern('validate-authorization')
async validateToken(data: { access_token: string; options?: any }) {
  // Your token validation logic here
  return userData;
}

🛡️ Error Handling

The function throws errors in the following cases:

  • Missing Authorization Header: When no Authorization header is present
  • Missing Client: When the microservice client is not provided
  • Validation Failure: When the microservice returns an error
try {
  const user = await validateAuth(req, client);
} catch (error) {
  if (error.message === 'Authorization header is missing') {
    // Handle missing header
  } else if (error.message === 'RmqClient is missing') {
    // Handle missing client
  } else {
    // Handle validation errors
  }
}

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the ISC License - see the LICENSE file for details.

👨‍💻 Authors

  • @antonin686
  • @SampadSikder

⭐ Star this repo if you find it helpful!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •