Back

Google Sign-In Redirects Back to Account Selection Instead of Closing the WebView

  • 1
  • Self Hosted
  • Auth
  • Flutter
COCAINE
29 Nov, 2024, 16:16

Hello Appwrite Support Team,

I am implementing Google Sign-In in my Flutter app. Everything works perfectly on the emulator. However, on my physical device, when I use the "Sign in with Google" option, it opens an in-app browser to select a Google account.

After successfully signing in and selecting the account, the in-app browser redirects back to the account selection page instead of closing. This behavior differs from the expected outcome where, upon successful authentication, the browser should close, and the app should resume.

I have already declared the necessary activity in the AndroidManifest.xml file.

Here is some code I’ve tried:

TypeScript
            <intent-filter android:label="flutter_web_auth_2">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="appwrite-callback-6749dd62000bc24eac8a" />
            </intent-filter>
        </activity>```

**service.dart**

``` //* Login a user using google
  Future<dynamic> continueWithGoogle() async {
    try {
      await account?.createOAuth2Session(
        provider: OAuthProvider.google,
        scopes: [
          "profile",
          "email",
        ],
      );
      return true;
    } catch (e) {
      return e.toString();
    }
  }```

**signinmethod.dart**

Due to text limit restriection I can't share signinmethod.dart here, Check out my Google docs for signinmethod.dart code: https://docs.google.com/document/d/1kYKQKKxJagkmimhpolxQFhQ59t9GDuRLOV-me_N6EFs/edit?usp=sharing
TL;DR
Issue with Google Sign-In redirecting back to account selection instead of closing the WebView. Make sure to try routing just after `await account.createOAuthSession` in the code. Check AndroidManifest.xml for proper configuration. Here's a possible solution: In the `auth_service.dart` file, modify the `googleSignIn()` method to handle the Google Sign-In process properly. Check the implementation of `account.createOAuth2Session(provider: OAuthProvider.google)` to ensure it handles the redirection correctly. Also, consider creating a separate Github issue to track and resolve this bug.
COCAINE
29 Nov, 2024, 16:24

Device Details:

Model: Redmi Note 10 Pro Brand: Xiaomi Android Version: 13

Utibe
15 Dec, 2024, 22:17

I have this same issue; I hope we get a solution as soon as possible.

D5
15 Dec, 2024, 22:44

What's the continuewithgoogle function?

D5
15 Dec, 2024, 22:44

What's your code?

D5
15 Dec, 2024, 22:45

I recommend creating a separated issue

Utibe
15 Dec, 2024, 23:18

I have this:

app_write_client.dart:

TypeScript
Client appWriteClient = Client().setEndpoint('https://cloud.appwrite.io/v1').setProject('6758162500220388bb27');
Account account = Account(appWriteClient);

auth_service.dart:

TypeScript
@singleton
class AuthenticationService {

  Future<User> googleSignIn() async{
    try {
      final response = await account.createOAuth2Session(provider: OAuthProvider.google);

      AppLogger.debug('googleSignIn response: ${response.toString()}');
      final user = await account.get();
      AppLogger.debug('googleSignIn User details: ${user.toMap()}');
      return user;
    } on AppwriteException catch (error, stackTrace) {
      AppLogger.error("googleSignIn Error: $error", stackTrace);
      throw error.toString();
    }
  }

}
Utibe
15 Dec, 2024, 23:18

in my viewmodel, i have this:

TypeScript
    startLoader();
    try {
      await authenticationService.googleSignIn();
      // await Future.delayed(const Duration(microseconds: 500));
      navigationService
          .navigateToAndRemoveUntilWidget(const AppBottomNavScreen(
        selectedIndex: 0,
      ));

      AppLogger.debug("Google Login Success :::");
      showCustomToast("Google Login Successful", success: true);
    } catch (err, s) {
      AppLogger.error("Google Login Error ::: ${err.toString()}", s);
      showCustomToast(err.toString());
    } finally {
      stopLoader();
    }
  }```
Utibe
15 Dec, 2024, 23:20

AndroidManifest.xml:

TypeScript
...
<activity android:exported="true" android:name="com.linusu.flutter_web_auth_2.CallbackActivity" >
            <intent-filter android:label="flutter_web_auth_2">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="appwrite-callback-6758162500220388bb7" />
            </intent-filter>
        </activity>
...
D5
15 Dec, 2024, 23:25

Try routing just after the await account.createOAuthSession Code

D5
17 Dec, 2024, 00:00

@COCAINE @Utibe Is that working?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more