Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
imwilliamxy opened this issue Jun 15, 2024 · 23 comments
Assignees
Labels
bug Something isn't working

Comments

@imwilliamxy
Copy link

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.
#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

  • I have read and followed the entire troubleshooting guide and it has not provided the solution I need.
  • I have provided all the information I can.
@imwilliamxy imwilliamxy added the bug Something isn't working label Jun 15, 2024
@ThexXTURBOXx
Copy link
Owner

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

@imwilliamxy
Copy link
Author

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
Copy link
Owner

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
Copy link

tmarkovski commented Jun 16, 2024

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
Copy link
Owner

@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
Copy link

tmarkovski commented Jun 16, 2024

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
Copy link

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

@ThexXTURBOXx
Copy link
Owner

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
Copy link

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

@ThexXTURBOXx
Copy link
Owner

@irfan512 I am aware of the issue :)

@joaquingrech
Copy link

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

@irfan512
Copy link

irfan512 commented Jun 22, 2024

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

@joaquingrech
Copy link

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
Copy link
Owner

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

@joaquingrech
Copy link

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).

@ThexXTURBOXx
Copy link
Owner

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

@joaquingrech
Copy link

so what do we do about this? The temporary fix is to use singleTop and document it on the instructions. But I don't see why it should be enforced to singleTop

@ThexXTURBOXx
Copy link
Owner

@joaquingrech Since I am unable to reproduce the issue on my app, I can't really do anything against it currently.
I am open to PRs, though, of course.

@bekirtaskin
Copy link

I have the same issue. I am using keycloak, tab not closing but code and token works fine at the backgroud. If session is already available in Keycloak, without asking username and password keycloak retuns code and tab closing by itself as expected.

@ThreeSpinningAxes
Copy link

Been trying to solve this issue but nothing. @tmarkovski do you have any updates if you resolved the problem?

@feeza10250
Copy link

feeza10250 commented Jul 5, 2024

const html = '''
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Grant Access to Flutter</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    html, body { margin: 0; padding: 0; }

    main {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      min-height: 100vh;
      font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
    }

    #icon {
      font-size: 96pt;
    }

    #text {
      padding: 2em;
      max-width: 260px;
      text-align: center;
    }

    #button a {
      display: inline-block;
      padding: 6px 12px;
      color: white;
      border: 1px solid rgba(27,31,35,.2);
      border-radius: 3px;
      background-image: linear-gradient(-180deg, #34d058 0%, #22863a 90%);
      text-decoration: none;
      font-size: 14px;
      font-weight: 600;
    }

    #button a:active {
      background-color: #279f43;
      background-image: none;
    }
  </style>
</head>
<body>
  <main>
    <div id="icon">&#x1F3C7;</div>
    <div id="text">Press the button below to sign in using your localhost account.</div>
    <div id="button"><a href="CALLBACK_URL_HERE" onclick="closeWin()">Sign in</a></div>
  </main>
</body>
<script>
function closeWin(){
window.close()
}
</script>
</html>
''';


- add function 

<script>
function closeWin(){
window.close()
}
</script>

- add onclick  

 <div id="button"><a href="CALLBACK_URL_HERE" onclick="closeWin()">Sign in</a></div>

@nipzu
Copy link

nipzu commented Jul 5, 2024

This seems to be related to task affinity. I had android:taskAffinity=”” set for my main activity in AndroidManifest.xml. Removing it fixed the issue.

@ThreeSpinningAxes
Copy link

This seems to be related to task affinity. I had android:taskAffinity=”” set for my main activity in AndroidManifest.xml. Removing it fixed the issue.

Can confirm this worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants