dreampowder / strava_flutter

Flutter package to use Strava v3 API
Other
37 stars 52 forks source link

Fails to auth on Android #87

Closed ChrisElliotUK closed 1 year ago

ChrisElliotUK commented 1 year ago

Hi, I have found that when our users go to through the auth process on android they get the option to open the redirect url in our app or chrome. If they open in chrome the auth process doesn't ever complete. Do you know how to resolve this?

dreampowder commented 1 year ago

Hi @ChrisElliotUK , i was planning to work on a similar bug report on my app too, will let you know when i take a look at that one

ChrisElliotUK commented 1 year ago

OK I fixed this by rewriting the auth to use flutter_web_auth.

Future<String> _getStravaCode(
      {required String redirectUrl,
      required List<AuthenticationScope> scopes,
      required bool forceShowingApproval,
      required String callbackUrlScheme}) async {
    final Completer<String> completer = Completer<String>();
    final params =
        '?client_id=${sl<SessionManager>().clientId}&redirect_uri=$redirectUrl&response_type=code&approval_prompt=${forceShowingApproval ? "force" : "auto"}&scope=${AuthenticationScopeHelper.buildScopeString(scopes)}';

    const authorizationEndpoint = "https://www.strava.com/oauth/mobile/authorize";

    final reqAuth = authorizationEndpoint + params;
    try {
      final result = await FlutterWebAuth.authenticate(
        url: reqAuth,
        callbackUrlScheme: callbackUrlScheme,
      );

      final parsed = Uri.parse(result);

      final error = parsed.queryParameters['error'];
      final code = parsed.queryParameters['code'];
      if (error != null) {
        completer.completeError(Fault(errors: [], message: error));
      } else {
        completer.complete(code);
      }
    } catch (e) {
      completer.completeError(Fault(errors: [], message: e.toString()));
    }
    return completer.future;
  }
ChrisElliotUK commented 1 year ago

Hey @dreampowder good timing, just finished.

The callbackurlscheme should be the first bit of your redirect url. e.g. redirectUrl: yourapp://www.redirect.url callbackurlscheme: yourapp

dreampowder commented 1 year ago

thanks a lot for the feedback 👍🏼 i'll check on my side too

ChrisElliotUK commented 1 year ago

Opened a pr

ChrisElliotUK commented 1 year ago

@dreampowder we have released my fix on our live app and it is working well for our users

dreampowder commented 1 year ago

Thanks a lot for the feedback, i'll apply it into this code too

Thanks for contributing!

dreampowder commented 1 year ago

Fixed with PR #88

btanev commented 1 year ago

Hi @dreampowder @ChrisElliotUK Are there any configuration changes that needs to be updated to use FlutterWebAuth correctly?

Since this PR was merge on Android I cannot authenticate with Strava. When I press the Strava "Authorize" button the response is always:

PlatformException(CANCELED, User canceled login, null, null)

Tested on emulators: Pixel 4 (API 30), Pixel 5 (API 31), Pixel 6 (API 33). It work well on iOS.

One thing that we lost with this PR and the old approach with using url_launch is that if we have the Strava app installed the authentication request was handled by the Strava app. Now it's always in an in-app web view.

btanev commented 1 year ago

It seem like we have to update the AndroidManifest.xml with:

    <application>
        ...
        <activity
            android:name="com.linusu.flutter_web_auth.CallbackActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="callback-scheme" />
            </intent-filter>
        </activity>
        ...
    </application>

However now after "Authorize" or "Cancel" actions in the Strava web view the web view is not automatically closed.

Did you guys experience similar problems in your test cases? Just wondering if i'm not missing anything else.

ChrisElliotUK commented 1 year ago

Hi yeah sorry I forgot to mention the package has additional set up required as I was already using it for something else.

btanev commented 1 year ago

The reason why the FlutterWebAuth activity was not closing was because my main Flutter activity was set to be android:launchMode="singleInstance". It was working better this way with url_launch. When I've reverted it back to singleTop it works.

What's probably left not is to configure the theme of the com.linusu.flutter_web_auth.CallbackActivity as it's all black now when creating.

During the weekend or next week if / when I have some time I'll prepare a PR at least to update the readme file.

pedropimont commented 1 year ago

Did anyone manage to solve this issue? I've followed those new instructions and still having the same problem. Thanks

PlatformException(CANCELED, User canceled login, null, null)

dreampowder commented 1 year ago

Have you tried the solution that provided previously?

dreampowder commented 1 year ago

@pedropimont i'll check, i was on an extremely busy work schedule, but i think i'll have time to polish lots of thins in this library this weekend.

francesc3000 commented 1 year ago

Did anyone manage to solve this issue? I've followed those new instructions and still having the same problem. Thanks

PlatformException(CANCELED, User canceled login, null, null)

I am experiencing this same error. I have implemented the proposed workarounds and always get the same error. Did anyone manage to solve this issue?

Thanks

francesc3000 commented 1 year ago

Did anyone manage to solve this issue? I've followed those new instructions and still having the same problem. Thanks PlatformException(CANCELED, User canceled login, null, null)

I am experiencing this same error. I have implemented the proposed workarounds and always get the same error. Did anyone manage to solve this issue?

Thanks

Solved! I have already been able to solve it. As explained above, this piece of code must be inserted into AndroidManifest.xml as it is. `<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" android:exported="true">

   </activity>`

In my case, I had the intent-filter declared inside another activity and that is what was failing me. I hope it can help someone else. Greetings.

dreampowder commented 1 year ago

yeah i was suspecting about this on people asking for the solution. Generally if the app does not return back from a launched intent, you shuold look for manifest settings as your app has to be registered through manifest in order to return open from its url scheme