flutter_web_auth doesn't redirect from WebView to the app after authorizing the access to my data in Flutter

Issue

First of all, I’m trying to get an authorization code by authenticating my account to my app.
To do this I used this package flutter_web_auth.
After implementing all what’s described on the page of the package , and opening the WebView to authorize my app to access my account data, I don’t get redirected to the app from WebView, here are the changes I made to AndroidManifest.xml and my code:

android/app/src/main/AndroidManifest.xml :

   <application
        android:label="swiftycompanion"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
        ( SOME CODE I HIDDEN THAT COME WITH THE INITIAL APP TO SIMPLIFY THE CODE )
        </activity>
       <activity
           android:exported="true"
           android:name="com.linusu.flutter_web_auth.CallbackActivity">
           <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.example.swiftycompanion://redirect" />
           </intent-filter>
       </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

And here is my code:

Future getAccessTokenWithAuthorizationCodeFlow() async {
  String url = 'https://api.intra.fr/oauth/authorize'
      '?client_id=MY_CLIENT_ID'
      '&redirect_uri=com.example.swiftycompanion%3A%2F%2Fredirect'
      '&response_type=code';
  try {
    final String result = await FlutterWebAuth.authenticate(
      url: url,
      callbackUrlScheme: 'com.example.swiftycompanion://redirect'
    );
    final String? token = Uri.parse(result).queryParameters['token'];
  } catch (e) {
    print(e);
  }
}

Solution

I use the same package, as android:scheme just add your package name without the ://redirect in the end.

Please, see the example below:

<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.example.swifty_companion" />
       </intent-filter>

Answered By – Prodromos Sarakinou

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.