waitForCallback method

Future<AuthCallbackResult> waitForCallback()

Start the server and wait for callback

Implementation

Future<AuthCallbackResult> waitForCallback() async {
  try {
    _server = await HttpServer.bind(InternetAddress.loopbackIPv4, _port);

    // Set up timeout
    _timeoutTimer = Timer(_timeout, () {
      if (!_completer.isCompleted) {
        _completer.complete(AuthCallbackResult(
          error: 'Authentication timed out after 5 minutes',
        ));
        stop();
      }
    });

    // Listen for requests
    await for (final request in _server!) {
      if (_completer.isCompleted) break;

      if (request.uri.path == '/callback') {
        await _handleCallback(request);
        break;
      } else {
        // Unknown route
        request.response
          ..statusCode = HttpStatus.notFound
          ..headers.contentType = ContentType.html
          ..write(_buildErrorHtml('Unknown route'));
        await request.response.close();
      }
    }
  } catch (e) {
    if (!_completer.isCompleted) {
      _completer.complete(AuthCallbackResult(error: e.toString()));
    }
  }

  return _completer.future;
}