Network Services
Overview
The @nx/core package provides IdentityNetworkService -- a cross-service HTTP client for authenticating users against the Identity Service. It extends the IGNIS framework's AxiosNetworkRequest helper and is automatically registered by DefaultApplication.
Source: packages/core/src/services/identity-network.service.ts
See the IGNIS Network Helper reference for details on the
AxiosNetworkRequestbase class.
Architecture
Class Hierarchy
IdentityNetworkService
Purpose
Communicates with the Identity Service for authentication. Its primary role is to support the Basic authentication strategy in DefaultApplication: when a controller uses @authenticate(['basic']), the credentials are forwarded to the Identity Service via this service.
Class Definition
import { EnvironmentKeys } from '@/common';
import { applicationEnvironment } from '@venizia/ignis';
import { AxiosNetworkRequest } from '@venizia/ignis-helpers/axios';
export class IdentityNetworkService extends AxiosNetworkRequest {
constructor() {
super({
name: IdentityNetworkService.name,
networkOptions: {
baseUrl: applicationEnvironment.get<string>(
EnvironmentKeys.APP_ENV_IDENTITY_SERVICE_BASE_URL,
),
},
});
}
async signIn(opts: {
identifier: { scheme: string; value: string };
credential: { scheme: string; value: string };
}) {
const networkService = this.getNetworkService();
const response = await networkService.post({
url: '/auth/sign-in',
body: opts,
});
return response.data;
}
}signIn Method
Authenticates a user by posting credentials to the Identity Service's /auth/sign-in endpoint.
Parameters:
| Field | Type | Description |
|---|---|---|
identifier.scheme | string | Identifier type: 'username', 'email', 'phone' |
identifier.value | string | The identifier value (e.g., 'john.doe') |
credential.scheme | string | Credential type: 'basic' (password) |
credential.value | string | The credential value (e.g., the password) |
Returns: The response data from the Identity Service (user context with tokens).
Example:
const result = await identityNetworkService.signIn({
identifier: {
scheme: 'username',
value: 'john.doe',
},
credential: {
scheme: 'basic',
value: 'mypassword',
},
});How It Is Used by DefaultApplication
DefaultApplication.configureSecurity() binds a verifyCredentials function that delegates to IdentityNetworkService.signIn(). This means any service extending DefaultApplication can use Basic auth without additional configuration:
// Inside DefaultApplication.configureSecurity()
this.bind<IBasicTokenServiceOptions>({
key: AuthenticateBindingKeys.BASIC_OPTIONS,
}).toValue({
verifyCredentials: async (opts) => {
const identityNetworkService = this.get<IdentityNetworkService>({
key: 'services.IdentityNetworkService',
});
return identityNetworkService.signIn({
identifier: { scheme: 'username', value: opts.credentials.username },
credential: { scheme: 'basic', value: opts.credentials.password },
});
},
});Authentication Flow
Registration
IdentityNetworkService is registered automatically in DefaultApplication.configureServices():
configureServices(): void {
this.service(IdentityNetworkService);
}It is bound in the DI container under the key services.IdentityNetworkService.
Injecting in Custom Services
import { inject, BaseService } from '@venizia/ignis';
import { IdentityNetworkService } from '@nx/core';
export class MyService extends BaseService {
constructor(
@inject({ key: 'services.IdentityNetworkService' })
private identityNetworkService: IdentityNetworkService,
) {
super({ scope: MyService.name });
}
async verifyUser(username: string, password: string) {
return this.identityNetworkService.signIn({
identifier: { scheme: 'username', value: username },
credential: { scheme: 'basic', value: password },
});
}
}Environment Variables
| Variable | Required | Description | Example |
|---|---|---|---|
APP_ENV_IDENTITY_SERVICE_BASE_URL | Yes | Base URL of the Identity Service | http://localhost:3001 |
Creating Custom Network Services
To communicate with other services, follow the same pattern:
import { EnvironmentKeys } from '@/common';
import { applicationEnvironment } from '@venizia/ignis';
import { AxiosNetworkRequest } from '@venizia/ignis-helpers/axios';
export class MyCustomNetworkService extends AxiosNetworkRequest {
constructor() {
super({
name: MyCustomNetworkService.name,
networkOptions: {
baseUrl: applicationEnvironment.get<string>('APP_ENV_MY_SERVICE_BASE_URL'),
},
});
}
async fetchData(id: string) {
const networkService = this.getNetworkService();
const response = await networkService.get({ url: `/data/${id}` });
return response.data;
}
}Register it in your application:
override configureServices(): void {
super.configureServices();
this.service(MyCustomNetworkService);
}Related Documentation
- IGNIS Network Helper Reference
- DefaultApplication -- security configuration that uses this service
- Core Components Overview