authInterceptor property

  1. @override
Interceptor get authInterceptor
override

Returns the dio.Interceptor that is used by APIs to inject the necessary auth headers for APIs that are authorized by this identity provider.

Implementation

@override
dio.Interceptor get authInterceptor => dio.InterceptorsWrapper(
      onRequest: (
        dio.RequestOptions options,
        dio.RequestInterceptorHandler handler,
      ) async {
        try {
          // Fetch the current auth session
          final session = await _cognitoPlugin.fetchAuthSession();
          if (session.isSignedIn) {
            options.headers['Authorization'] = 'Bearer '
                '${session.userPoolTokensResult.value.accessToken.raw}';
          }

          return handler.next(options);
        } catch (error, stackTrace) {
          _logger.severe(
            'Failed to fetch AWS Cognito JWT for request: ${options.uri}',
            error,
            stackTrace,
          );
          signOut();

          throw AuthTokenException(
            message: 'Failed to fetch AWS Cognito JWT for request',
            innerException: error is Exception ? error : Exception(error),
            innerStackTrace: stackTrace,
          );
        }
      },
    );