UNPKG

@10abdullahbutt/auth-module

Version:

A NestJS-style authentication module with JWT and role-based access control.

92 lines (70 loc) 1.6 kB
# @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 ```bash npm install @10abdullahbutt/auth-module ``` ## Usage ### Import the Module ```typescript 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: ```typescript import { JwtModule } from '@nestjs/jwt'; @Module({ imports: [ JwtModule.register({ secret: process.env.JWT_SECRET, signOptions: { expiresIn: '1h' }, }), ], }) export class AppModule {} ``` ### Protect Routes with JWT ```typescript 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 ```typescript 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 ```bash npm run test ``` ## License MIT