Skip to content
Back

Need help with OAuth Google in Flutter App (Android)

  • 0
  • Auth
Rakibul Hasan
9 Apr, 2025, 15:01

What I Wanted- I am making an app where I have a button "Signin with google" where clicking on it will redirect me to google account page and select and account there. When it is successful it will navigate to BoardWindow.

What happens- When I click on the "Sign in with google button" it redirects me to google account page. When I select an account it shows appwrite logo then again goes to google account page. And this loop keeps repeating.

Code-

TypeScript
@override
  void initState() {
    super.initState();

    checkLoginStatus();
  }

  Future<void> checkLoginStatus() async {
    if (await isLoggedIn()) {
      print('logged in');
      WidgetsBinding.instance.addPostFrameCallback((_) {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => BoardWindow(account: account),
          ),
        );
      });
    } else {
      print("logged out");
    }
  }

  Future<void> signInWithGoogle(BuildContext context) async {
    try {
      await account.createOAuth2Session(provider: OAuthProvider.google);
      // Wait for login to complete and then check session
      await Future.delayed(Duration(seconds: 2)); // Let redirect complete
      if (await isLoggedIn()) {
        // If login was successful, go to BoardWindow
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
              builder: (context) => BoardWindow(account: account)),
        );
      }
    } catch (e) {
      print("OAuth login error: $e");
    }
  }
TypeScript
ElevatedButton(
                  onPressed: () {
                    signInWithGoogle(context);
                  },

I have also added the activity code for AndroidManifest.xml with project-id in appwrite...

what am i doing wrong?

TL;DR
Developers are encountering issues with OAuth Google in a Flutter app. One developer shared a solution: Update the flutter_web_auth_2 package to version 5.0.0-alpha.1 and follow recommendations to set android:launchMode="singleTop" and remove android:taskAffinity entries. This should resolve the problem of being stuck in a loop after selecting a Google account. Also ensure the account session is handled correctly in the code snippets provided.
Rakibul Hasan
9 Apr, 2025, 15:13

For anyone facing this issue

TypeScript
Hi everyone! I also ran into this issue today, and I think I managed to solve it.
    
I updated the flutter_web_auth_2 package to the latest version 5.0.0-alpha.1.
You can do this by adding the following to your pubspec.yaml under dependency_overrides:
dependency_overrides:
  flutter_web_auth_2: 5.0.0-alpha.1

I also found this recommendation in the flutter_web_auth_2 package description:
There is also a known problem with task affinities and launch modes (see #113). In order to ensure that flutter_web_auth_2 works correctly, set android:launchMode="singleTop" and remove any android:taskAffinity entries. This configuration is guaranteed to work.
```

Courasy by Egor Tabula
Raman
9 Apr, 2025, 15:33

You can solve the flutter_web_auth_2 problem by following this issue. https://discord.com/channels/564160730845151244/1334415894394306571/1334427575790997546

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