appwrite / sdk-for-flutter

[READ-ONLY] Official Appwrite Flutter SDK 💙
https://appwrite.io
BSD 3-Clause "New" or "Revised" License
364 stars 109 forks source link

🐛 Bug Report: [Android] Switching to any authenticator app while creating a Google OAuth Session breaks the login flow #181

Open metalwings opened 10 months ago

metalwings commented 10 months ago

👟 Reproduction steps

Requirements

Reproduction

  1. Enable Google 2FA and make sure you are using an android device
  2. Login into your google account through createOAuth2Session(provider: "google")
  3. You switch to your 2FA application
  4. After confirming your 2FA, the login window is automatically closed but you're not logged in

Further information

Article how to add Google 2FA to your account: https://support.google.com/accounts/answer/185839

I've created a minimal reproduction repository: https://gitlab.com/metalwings/appwrite-flutter-oauth2-demo

In my case the default "is this you?" screen is automatically shown by android as soon as I try to login into my account.

👍 Expected behavior

Switching to any authenticator app doesn't break the login flow. Users should be able to switch to other app (in my case the default android auth by google, check my 2FA Code (or press "that's me") and login after I've completed the 2FA flow

👎 Actual Behavior

The login flow is cancelled due to loss of app focus. An exception is thrown:

E/flutter ( 6554): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(CANCELED, User canceled login, null, null)
E/flutter ( 6554): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:652:7)
E/flutter ( 6554): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:310:18)
E/flutter ( 6554): <asynchronous suspension>
E/flutter ( 6554): #2      FlutterWebAuth2MethodChannel.authenticate 
(package:flutter_web_auth_2_platform_interface/method_channel/method_channel_flutter_web_auth_2.dart:16:7)

🎲 Appwrite version

Version 2.0.x Appwrite Cloud SDK Version 11

💻 Operating system

Linux

🧱 Your Environment

Problem can be reproduced in the appwrite cloud version

👀 Have you spent some time to check if this issue has been raised before?

🏢 Have you read the Code of Conduct?

metalwings commented 10 months ago

Playing around with the android:launchMode in https://gitlab.com/metalwings/appwrite-flutter-oauth2-demo/-/blob/main/android/app/src/main/AndroidManifest.xml?ref_type=heads#L9 as suggest by @stnguyen90 didn't help.

metalwings commented 10 months ago

Tested on iOS. Everything works there, because the sign-in window behavior is different.

Also found a new clue: For some reason, the browser window "Sign in with Google" is closed automatically as soon as I try to switch to another app.

https://github.com/appwrite/sdk-for-flutter/assets/6256062/97e719c2-1f23-40f0-849d-db91f00a3d2d

This behavior can be reproduced on different physical Android devices and on emulated devices as well.

metalwings commented 10 months ago

Workaround:

String provider = "google";
String projectId = "<YOUR PROJECT ID HERE>";
String host = "https://cloud.appwrite.io/v1";
String url = "$host/account/sessions/oauth2/$provider?project=$projectId";
try {
  await FlutterWebAuth2.authenticate(
      url:url,
      callbackUrlScheme: "appwrite-callback-$projectId");
  await widget.account.createOAuth2Session(provider: provider);
} catch (e) {
  print("Do nothing or handle exception, because the login was not successful");
}