Robust authentication and authorization module for NestJS applications, supporting multiple strategies (including Keycloak and custom services), guards, decorators, and middleware for the Edirect platform.
Robust authentication and authorization module for NestJS applications, supporting multiple strategies (including Keycloak and custom services), guards, decorators, and middleware for the Edirect platform.
- Plug-and-play authentication for Edirect microservices
- Keycloak and custom service support
- Guards, middleware, and decorators for fine-grained access control
- Integrates with @edirect/config and other Edirect modules
- Type-safe interfaces for user and token management
``bash`
npm install @edirect/auth
`typescript
import { AuthModule, AuthGuard, Permissions, Roles } from '@edirect/auth';
@Module({
imports: [AuthModule],
controllers: [MyController],
})
export class MyAppModule {}
@Controller('example')
export class MyController {
@Get()
@UseGuards(AuthGuard)
@Permissions('read:data')
@Roles('admin')
getData(@Req() req) {
return req.user;
}
}
`
- AuthModule, AuthService, AuthGuard, AuthMiddlewareKeycloakAuthModule
- , KeycloakAuthGuard, KeycloakAuthMiddleware, KeycloakAuthTokenExchangeMiddlewarePermissions
- , Roles, Resources (decorators)AUTH_SERVICE_TOKEN
- , PermissionsEnum, RolesEnum (constants)UserInterface
- Type interfaces: , EntityInterface, AuthenticatedRequestInterface, etc.
MIT
The module relies on environment variables for configuration, especially when integrating with Keycloak or custom authentication providers. These variables can be set globally or per realm (multi-tenant support):
- AUTH_SERVICE_URL: Base URL for the authentication service (custom provider).AUTH_SERVICE_TOKEN
- : Service token or secret for internal authentication.
- KEYCLOAK_BASE_URL: Keycloak server base URL.KEYCLOAK_REALM
- : Keycloak realm name.KEYCLOAK_CLIENT_ID
- : Client ID registered in Keycloak.KEYCLOAK_CLIENT_SECRET
- : Client secret for the client.KEYCLOAK_TIMEOUT
- : (Optional) Timeout for Keycloak requests.
#### Multi-Realm Support
For multi-tenant scenarios, variables can be set per realm using the pattern:
```
KEYCLOAK_
KEYCLOAK_
KEYCLOAK_
The module will resolve the correct variable based on the current realm context.
> Tip: Use @edirect/config to manage and load environment variables securely and consistently across your services.