localHttpRedirector function

PlanningCenterAuthRedirector localHttpRedirector(
  1. String redirectUri
)

localHttpRedirector is a function that builds an example Auth Redirector using the specified ip address and port. It will open an http server listening at that address and port waiting for the user's browser to perform the redirect.

Implementation

PlanningCenterAuthRedirector localHttpRedirector(String redirectUri) {
  var redirectUriData = Uri.parse(redirectUri);
  var host = redirectUriData.host;
  var port = redirectUriData.port;
  Future<String> r(String url) async {
    var completer = Completer<String>();
    var server = await HttpServer.bind(host, port);
    server.listen((HttpRequest req) async {
      req.response.write('Thanks! You can close this window now.');
      req.response.close();
      server.close();
      print(req.requestedUri);
      print(req.requestedUri.queryParameters);
      completer.complete(req.requestedUri.queryParameters['code'] ?? '');
    });

    print('visit the following url in your browser');
    print(url);

    // Once the user is redirected to `redirectUrl`, pass the query parameters to
    // the AuthorizationCodeGrant. It will validate them and extract the
    // authorization code to create a new Client.
    return completer.future;
  }

  return r;
}