MaikuB / flutter_appauth

A Flutter wrapper for AppAuth iOS and Android SDKs
269 stars 238 forks source link

authorizeAndExchangeCode not working as expected #423

Closed msh-nayan closed 1 year ago

msh-nayan commented 1 year ago

Hello, Greetings. Thanks for the awesome library.

I built the example project with your given credential. It was working nicely.

But when I'm using my own credential, authorizeAndExchangeCode is not working as expected. Consider the following scenario

Future<void> _signInWithAutoCodeExchange(
      {bool preferEphemeralSession = false}) async {
    try {
      _setBusyState();

      // show that we can also explicitly specify the endpoints rather than getting from the details from the discovery document
      debugPrint('_signInWithAutoCodeExchange >> before');
      final AuthorizationTokenResponse? result =
          await _appAuth.authorizeAndExchangeCode(
        AuthorizationTokenRequest(
          _clientId,
          _redirectUrl,
          // serviceConfiguration: _serviceConfiguration,
          discoveryUrl: _discoveryUrl,
          // issuer: _issuer,
          scopes: _scopes,
          preferEphemeralSession: preferEphemeralSession,
        ),
      ).catchError(() => print('error > not able to be reached'));
      debugPrint('_signInWithAutoCodeExchange >> result >> ${result == null}');

      if (result != null) {
        debugPrint('main >> pdl >> access_token >> ${result.accessToken} '
            '\n\n id_token >> ${result.idToken} '
            '\n\n refresh_token >> ${result.refreshToken} '
            '\n\n expire_date_time >> ${result.accessTokenExpirationDateTime!.toIso8601String()}');
        _processAuthTokenResponse(result);
        await _testApi(result);
      }
    } catch (_) {
      print(_); // prints >>> type '() => void' is not a subtype of type '(Object) => dynamic'
      debugPrint('_signInWithAutoCodeExchange >> catch >> $_'); // _signInWithAutoCodeExchange >> catch >> type '() => void' is not a subtype of type '(Object) => dynamic'
      _clearBusyState();
    }
  }

  void _processAuthTokenResponse(AuthorizationTokenResponse response) {
    setState(() {
      _accessToken = _accessTokenTextController.text = response.accessToken!;
      _idToken = _idTokenTextController.text = response.idToken!;
      _refreshToken = _refreshTokenTextController.text = response.refreshToken!;
      _accessTokenExpirationTextController.text =
          response.accessTokenExpirationDateTime!.toIso8601String();
    });
  }

Any help will be appreciated. Thank you.

MaikuB commented 1 year ago

This isn't a plugin issue but issue with your own Dart code. This is something you would need to look into further yourself. What sticks out to me is you have print(_), which isn't valid code so looks like you need to take a closer look at your code to ensure it compiles/works properly first