Skip to content

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 AxiosNetworkRequest base 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

typescript
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:

FieldTypeDescription
identifier.schemestringIdentifier type: 'username', 'email', 'phone'
identifier.valuestringThe identifier value (e.g., 'john.doe')
credential.schemestringCredential type: 'basic' (password)
credential.valuestringThe credential value (e.g., the password)

Returns: The response data from the Identity Service (user context with tokens).

Example:

typescript
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:

typescript
// 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():

typescript
configureServices(): void {
  this.service(IdentityNetworkService);
}

It is bound in the DI container under the key services.IdentityNetworkService.

Injecting in Custom Services

typescript
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

VariableRequiredDescriptionExample
APP_ENV_IDENTITY_SERVICE_BASE_URLYesBase URL of the Identity Servicehttp://localhost:3001

Creating Custom Network Services

To communicate with other services, follow the same pattern:

typescript
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:

typescript
override configureServices(): void {
  super.configureServices();
  this.service(MyCustomNetworkService);
}

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.