ThexXTURBOXx / flutter_web_auth_2

Flutter plugin for authenticating a user with a web service
https://pub.dev/packages/flutter_web_auth_2
MIT License
46 stars 46 forks source link

[Bug]: The browser window cannot close properly after logging in success on the Android physical device #113

Open imwilliamxy opened 1 week ago

imwilliamxy commented 1 week ago

Description

After successfully logging into the app, the browser window cannot be closed properly on Android devices. According to the logs, the app has already obtained the accessToken. The same operation works perfectly on iOS.

final result = await FlutterWebAuth2.authenticate(url: authUrl.toString(), callbackUrlScheme: "com.company.appname", options: const FlutterWebAuth2Options(intentFlags: ephemeralIntentFlags));

Similar links are here. https://github.com/ThexXTURBOXx/flutter_web_auth_2/issues/106

according this thread, flutter_web_auth_2 was upgraded to version ^4.0.0-alpha.0

Minimal Reproduction

Steps to reproduce the behaviour:

  1. Use the following code:
    final result = await FlutterWebAuth2.authenticate(url: authUrl.toString(), callbackUrlScheme: "com.company.appname", options: const FlutterWebAuth2Options(intentFlags: ephemeralIntentFlags));
  2. The following code was added to AndroidManifest.xml
    <activity
      android:name="com.linusu.flutter_web_auth_2.CallbackActivity"
      android:exported="true">
      <intent-filter android:label="App Login">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="ccom.company.appname"/>
      </intent-filter>
    </activity>

Exception or Error

There's no error log

Expected Behaviour

The web browser should close and app should come into focus

Screenshots

No response

Additional context

flutter doctor -v [✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-x64, locale en-US) • Flutter version 3.22.1 on channel stable at /Users/username/Documents/development/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision a14f74ff3a (3 weeks ago), 2024-05-22 11:08:21 -0500 • Engine revision 55eae6864b • Dart version 3.4.1 • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/username/Documents/development/Android/sdk • Platform android-34-ext8, build-tools 34.0.0 • ANDROID_HOME = /Users/username/Documents/development/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15F31d • CocoaPods version 1.15.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version unknown) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.90.0 ✗ Unable to determine VS Code version.

[!] Proxy Configuration • HTTP_PROXY is set ! NO_PROXY is not set

[✓] Connected device (4 available) • Pixel 6a (mobile) • codemasked • android-arm64 • Android 14 (API 34) • macOS (desktop) • macos • darwin-x64 • macOS 14.5 23F79 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.142

Device

pixel 6a

OS

Android 14

Browser

Chrome

Flutter version

Flutter version 3.22.1

flutter_web_auth_2 version

^4.0.0-alpha.0

Checklist

ThexXTURBOXx commented 1 week ago

Your AndroidManifest.xml contains <data android:scheme="ccom.company.appname"/> (note the ccom instead of com).

imwilliamxy commented 1 week ago

Thank you for your reply. The error you pointed out was due to my typo. When submitting this issue, for privacy reasons, I replaced the actual schema with ccom.company.appname. In the real production environment, the scheme included in the AndroidManifest.xml and the callbackUrlScheme parameter in the login code are exactly the same. I can confirm this for sure.

Thank you again for your quick response.

ThexXTURBOXx commented 1 week ago

Oh okay, sorry, should have known that... Could you maybe try fixing the version of flutter_web_auth_2 to 4.0.0-alpha.5 or to 4.0.0-alpha.4 and report back? It might be that either version 6 or 5 contains code that breaks some stuff on Android... Could also be an Android 14 issue, but I tought I had tested this before

tmarkovski commented 1 week ago

I am experiencing the same issue. I made a short video of my setup, very basic app with a custom scheme configured. I've tried multiple versions of the 4.0.0-alpha.X, they all have the same behavior. I'm using Android API 34. I have tried with and without passing ephemeralIntentFlags. I don't know if this is the same issue, happy to open a separate one if needed.

Flutter App Custom Scheme Setup - Watch Video

ThexXTURBOXx commented 1 week ago

@tmarkovski Thank you very much for the video - this is one of the most helpful bug reports I have ever received :) I will take a closer look at this. On the first button push it looked like it actually redirected to the app, but for some reason went back again (the small flicker indicates something like that)... Let's see

tmarkovski commented 1 week ago

this is one of the most helpful bug reports I have ever received

You're welcome, it is also the fastest way to describe a problem.

That's correct, it seems like it navigates back, but the custom tab is still there. Either way, the app is launched correctly and the result is returned by the authenticate function.

joaquingrech commented 5 days ago

anyone figured this out? I'm having the same issue on android device, after login the activity screen does not go away

ThexXTURBOXx commented 5 days ago

I'm sorry that I have not yet been able to look at it properly. I will update you once I had time to investigate

irfan512 commented 4 days ago

@ThexXTURBOXx Hi i am facing same issue after login the activity screen does not go away

ThexXTURBOXx commented 4 days ago

@irfan512 I am aware of the issue :)

joaquingrech commented 4 days ago

To add more information, it only happens on a physical Android device. On the emulator, it works without issue.

irfan512 commented 4 days ago

@joaquingrech on a physical Android device it not come back in app

joaquingrech commented 4 days ago

I think I found the issue. Can you paste your androidmanifest? the launch mode of your main activity? It seems it must be set to singleTop. Mine was set to singleInstance and when I switched to singleTop it began working.

How's yours?

ThexXTURBOXx commented 4 days ago

@joaquingrech Mine is also set to singleTop and works fine - could indeed be the root of the problem!

joaquingrech commented 4 days ago

I'm not sure why it would fail and I believe mine was set to singleInstance by some other plugin. In theory, that should not make this plugin fail unless it's creating multiple instances and overlapping each other. You may want to try with the different launch modes and see which ones work and which ones fail to add it to the documentation (or fix if possible).

irfan512 commented 4 days ago

@joaquingrech can you please paste your androidmanifest.

ThexXTURBOXx commented 3 days ago

@irfan512 You have a different issue: You have not read flutter_web_auth_2's setup instructions.