startAuthFlow method

Future<void> startAuthFlow()

Implementation

Future<void> startAuthFlow() async {
  // a scope is required for Microsoft
  List<String> scopes = ['openid'];
  if (widget._controller.initialScopes != null) {
    scopes.addAll(widget._controller.initialScopes!);
  }

  if (widget._controller.onInitiated != null) {
    ScreenController()
        .executeAction(context, widget._controller.onInitiated!);
  }

  OAuthServiceToken? token;
  try {
    token =
        await OAuthControllerImpl().authorize(context, OAuthService.microsoft,
            scope: ConnectUtils.getScopesAsString(scopes),
            forceNewTokens: true, // this always force the flow again
            tokenExchangeAPI: widget._controller.tokenExchangeAPI);

    // dispatch success
    if (widget._controller.onAuthorized != null && token != null) {
      ScreenController()
          .executeAction(context, widget._controller.onAuthorized!);
      return;
    }
  } catch (e) {
    if (e is PlatformException && e.code == 'CANCELED') {
      if (widget._controller.onCanceled != null) {
        ScreenController()
            .executeAction(context, widget._controller.onCanceled!);
      }
      return;
    }
  }

  // dispatch error
  if (widget._controller.onError != null && token == null) {
    ScreenController().executeAction(context, widget._controller.onError!);
  }
}