flutter_web_auth icon indicating copy to clipboard operation
flutter_web_auth copied to clipboard

Web auth receive token, but doesn't return to app

Open stryder123451 opened this issue 2 years ago • 2 comments

After successful authorization , i must close browser by myself. On iOS it works normal, and app open by itself

stryder123451 avatar Nov 27 '23 06:11 stryder123451

Might not be the same issue as we had but in our case on android, the issue was in the android manifest. Where the MainActivity is defined, there is a parameter called "android:launchMode". for us, it didn't redirect back until we set that to "singleTask"

kennydead avatar Apr 18 '24 08:04 kennydead

I managed to solve it in a different way:

  1. I kept the 'android:launchMode="singleTop"', as it was set when the app was created.
  2. I ended up removing the 'android:taskAffinity=""' property because I found in another source that it's recommended to remove this property to fix the issue.
  3. I changed the app ID in the path 'android/app/src/build.gradle', specifically the 'applicationId' property in the 'defaultConfig'. (This ID is a unique identifier for the app; you can set it to whatever you like.)
  4. My AndroidManifest now looks as follows:
<activity
    android:name="com.linusu.flutter_web_auth.CallbackActivity" 
    android:exported="true">
    <intent-filter android:label="flutter_web_auth">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="com.codecentauri.nps"
            android:host="oauth"
            android:path="/callback" />
    </intent-filter>
</activity>

Note that the value of the android:scheme property must match the ID you set in the build.gradle file.

  1. My method that opens the browser to perform the login is now as follows:
Future<void> authenticate() async {
    final grant = OAuth2.AuthorizationCodeGrant(
      clientId,
      authorizationEndpoint,
      tokenEndpoint,
      secret: clientSecret,
    );

    final authorizationUrl = grant.getAuthorizationUrl(
      Uri.parse(redirectUrl),
      scopes: scopes,
    );

    try {
      logger.i('Opening browser to authenticate the user.');

      final result = await FlutterWebAuth.authenticate(
        url: authorizationUrl.toString(),
        callbackUrlScheme: 'com.codecentauri.nps',
      );

      final code = Uri.parse(result).queryParameters['code'];
      if (code == null) {
        throw Exception('Authorization code not found.');
      }

      final client = await grant.handleAuthorizationResponse({'code': code});
      await _saveCredentials(client.credentials);
      _client = client;
    } on OAuth2.AuthorizationException catch (e) {
      logger.e('Error during authentication: $e');
    } on Exception catch (e) {
      logger.e('Unknown error during authentication: $e');
    }
  }

Ensure the callbackUrlScheme matches the ID defined in build.gradle.

  1. Finally, the value of my redirectUrl variable is 'com.codecentauri.nps://oauth/callback'.

Extra:
If you encounter issues when testing with the .apk in release mode, remember to add the following tag in the AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"/>

andregurgel avatar Nov 20 '24 20:11 andregurgel