election-shared-auth-package_v2
v0.1.7
Published
A shared authentication package for NestJS applications providing JWT and Google OAuth strategies, guards, and decorators for the election system microservices
Maintainers
Readme
Election Shared Auth Package V2
A comprehensive authentication package for NestJS applications, specifically designed for the election system microservices. This package provides JWT and Google OAuth authentication strategies, guards, decorators, and services.
Features
- 🔐 JWT Authentication Strategy
- 🌐 Google OAuth 2.0 Strategy
- 🛡️ Authentication Guards
- 🎨 Easy-to-use Decorators
- 📦 Ready-to-use Auth Module
- 🔧 TypeScript Support
Installation
npm install election-shared-auth-package_v2Quick Start
1. Import the Auth Module
import { Module } from '@nestjs/common';
import { AuthModule } from 'election-shared-auth-package_v2';
@Module({
imports: [AuthModule],
// ... your other module configuration
})
export class AppModule {}2. Use the Auth Decorator
import { Controller, Get } from '@nestjs/common';
import { Auth, User } from 'election-shared-auth-package_v2';
@Controller('protected')
export class ProtectedController {
@Get('profile')
@Auth('localJwt') // Apply JWT authentication
getProfile(@User() user: any) {
return { user };
}
}Available Components
Decorators
@Auth(strategy)
Applies authentication guard with the specified strategy.
@Auth('localJwt') // For JWT authentication
@Auth('google') // For Google OAuth@User(property?)
Extracts user information from the request.
@User() user: any // Get entire user object
@User('id') userId: string // Get specific user propertyStrategies
- JwtStrategy: Handles JWT token validation
- GoogleStrategy: Handles Google OAuth 2.0 authentication
Guards
- GoogleOAuthGuard: Guard for Google OAuth authentication
Services
- AuthService: Core authentication service with utility methods
Configuration
Make sure to configure your environment variables:
# JWT Configuration
JWT_SECRET=your-jwt-secret
JWT_EXPIRES_IN=3600s
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_CALLBACK_URL=your-callback-urlAPI Documentation
This package automatically adds Swagger/OpenAPI documentation for authentication endpoints:
- Bearer token authentication support
- Cookie authentication support
- Proper error responses
Contributing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
This project is licensed under the MIT License.
