@edirect/auth
v11.0.43
Published
Robust authentication and authorization module for NestJS applications, supporting multiple strategies (including Keycloak and custom services), guards, decorators, and middleware for the Edirect platform.
Maintainers
Keywords
Readme
@edirect/auth
Robust authentication and authorization module for NestJS applications, supporting multiple strategies (including Keycloak and custom services), guards, decorators, and middleware for the Edirect platform.
Features
- 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
Installation
npm install @edirect/authUsage Example
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;
}
}Main Exports
AuthModule,AuthService,AuthGuard,AuthMiddlewareKeycloakAuthModule,KeycloakAuthGuard,KeycloakAuthMiddleware,KeycloakAuthTokenExchangeMiddlewarePermissions,Roles,Resources(decorators)AUTH_SERVICE_TOKEN,PermissionsEnum,RolesEnum(constants)- Type interfaces:
UserInterface,EntityInterface,AuthenticatedRequestInterface, etc.
Integration Notes
License
MIT
Environment Variables
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):
Common Variables
AUTH_SERVICE_URL: Base URL for the authentication service (custom provider).AUTH_SERVICE_TOKEN: Service token or secret for internal authentication.
Keycloak Variables
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_<REALM>_CLIENT_ID=...
KEYCLOAK_<REALM>_CLIENT_SECRET=...
KEYCLOAK_<REALM>_BASE_URL=...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.
