@10abdullahbutt/auth-module
v1.0.0
Published
A NestJS-style authentication module with JWT and role-based access control.
Downloads
17
Maintainers
Readme
@10abdullahbutt/auth-module
A NestJS-style authentication module providing JWT authentication and role-based access control (RBAC).
Features
- JWT authentication guard
- Role-based access control guard
- Custom roles decorator
- Easily pluggable into any NestJS application
Installation
npm install @10abdullahbutt/auth-moduleUsage
Import the Module
import { Module } from '@nestjs/common';
import { AuthModule } from '@10abdullahbutt/auth-module';
@Module({
imports: [
AuthModule,
],
})
export class AppModule {}Configure JWT
Override the JwtModule.register options in your main app for secret and expiration:
import { JwtModule } from '@nestjs/jwt';
@Module({
imports: [
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: { expiresIn: '1h' },
}),
],
})
export class AppModule {}Protect Routes with JWT
import { Controller, Get, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '@10abdullahbutt/auth-module';
@Controller('profile')
export class ProfileController {
@UseGuards(JwtAuthGuard)
@Get()
getProfile() {
// ...
}
}Role-based Access
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Roles, RolesGuard, JwtAuthGuard } from '@10abdullahbutt/auth-module';
@Controller('admin')
@UseGuards(JwtAuthGuard, RolesGuard)
export class AdminController {
@Roles('admin')
@Get()
getAdminData() {
// ...
}
}Testing
npm run testLicense
MIT
