onRequest method

  1. @override
void onRequest(
  1. RequestOptions options,
  2. RequestInterceptorHandler handler
)

Called when the request is about to be sent.

Implementation

@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  // Check if the authentication bloc is registered.
  if (GetIt.instance.isRegistered<AuthenticationBloc>()) {
    // Get the authentication bloc.
    final authenticationBloc = GetIt.instance.get<AuthenticationBloc>();

    // Check if the user is authenticated.
    if (authenticationBloc.state.isAuthenticated) {
      // Get the language from the user.
      final language = (authenticationBloc.state
              as UserAuthenticatedWithSelectedTenantState)
          .user
          .language
          .value
          .code
          .value;
      if (language.isNotEmpty) {
        options.headers['X-Language'] = language;
      }
    }
  }
  handler.next(options);
}