dpa-auth-client
v1.1.0
Published
NestJS module for inter-service authentication and permission checking with Auth Service
Maintainers
Readme
dpa-auth-client
NestJS module untuk inter-service authentication dan permission checking dengan Auth Service.
Installation
npm install dpa-auth-clientQuick Start
// app.module.ts
import { Module } from '@nestjs/common';
import { DpaAuthClientModule } from 'dpa-auth-client';
@Module({
imports: [
DpaAuthClientModule.forRoot({
baseUrl: 'http://auth-service:3000',
useGlobalGuards: true, // optional, default: true
}),
],
})
export class AppModule {}Usage
Decorators
import { Public, RequirePermissions, CurrentUser } from 'dpa-auth-client';
@Controller('store')
export class StoreController {
// Public endpoint (skip auth)
@Public()
@Get('health')
health() { return 'OK'; }
// Require permission
@RequirePermissions('store:linebar:read')
@Get('linebar')
getLinebar() { ... }
// Multiple permissions (ALL required)
@RequirePermissions('store:linebar:write', 'store:linebar:delete')
@Delete(':id')
delete(@CurrentUser() user: AuthenticatedUser) { ... }
}Manual Permission Check
import { AuthClientService } from 'dpa-auth-client';
@Injectable()
export class MyService {
constructor(private authClient: AuthClientService) {}
async doSomething(token: string) {
// Simple check
const canBypass = await this.authClient.hasPermission(
token,
'schedule.linebar.bypass_check_employee'
);
// Detailed check
const result = await this.authClient.checkPermission(token, [
'permission.one',
'permission.two',
]);
// result = { hasPermission, missingPermissions, isSuperAdmin }
}
}Configuration Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| baseUrl | string | required | Auth Service base URL |
| validateEndpoint | string | /api/auth/validate-token | Token validation endpoint |
| checkPermissionEndpoint | string | /api/auth/check-permission | Permission check endpoint |
| timeout | number | 5000 | Request timeout (ms) |
| retryAttempts | number | 3 | Number of retry attempts |
| retryDelay | number | 1000 | Delay between retries (ms) |
| useGlobalGuards | boolean | true | Register guards globally |
