Skip to content
Back

Google Oath2 for Flutter Android App not redirecting back to the app after successful authentication

  • 0
  • Android
  • Flutter
  • Auth
theos
21 Jul, 2025, 13:52

I have this function in my auth file for logging in via google Future<void> loginWithGoogle() async { try { final result = await FlutterWebAuth2.authenticate( url: 'https://fra.cloud.appwrite.io/v1/account/sessions/oauth2/google?project=685d2b28003a614fa813', callbackUrlScheme: 'com.example.xhale', );

TypeScript
  final uri = Uri.parse(result);
  final sessionId =
      uri.fragment
          .split('&')
          .firstWhere((e) => e.startsWith('sessionId='))
          .split('=')[1];

  await widget.account.updateSession(sessionId: sessionId);

  final user = await widget.account.get();
  final userId = user.$id;
  final email = user.email;
  final name = user.name;

  final userNotifier = ref.read(userProvider.notifier);

  final exists = await userNotifier.checkIfUserExists(userId);

  if (exists) {
    await userNotifier.fetchUserData(userId);
    if (!mounted) return;
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(builder: (_) => HomePage(account: widget.account)),
    );
  } else {
    await userNotifier.createNewUser(
      userId: userId,
      email: email,
      name: name,
      phoneNumber: null,
    );
    if (!mounted) return;
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder:
            (_) => InitialSmokingPreferencesPage(
              account: widget.account,
              email: email,
              name: name,
              phoneNumber: null,
            ),
      ),
    );
  }
} catch (e) {
  debugPrint('❌ Google login error: $e')
}

}

The AndroidManifest file and the google cloud client are configured, but after I pick the account, i stay stuck at the 'https://fra.cloud.appwrite.io/v1/console/auth/oath2/success?project=685d2b28003a614fa813' and not redirected to my app. Why?

TL;DR
Google Oath2 for Flutter Android App not redirecting back to the app after successful authentication. Check the AndroidManifest.xml code for the callback activity with the correct scheme. In the loginWithGoogle function, ensure the callbackUrlScheme matches the scheme in the AndroidManifest. Verify that the redirect URL after authentication matches the expected URL in your app.
theos
21 Jul, 2025, 13:54

I will add the relevant AndroidManifest.xml code here for reference: <activity android:name=".MainActivity" android:enableOnBackInvokedCallback="true" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- CallbackActivity για Google Sign-In -->

<!-- Add this inside the <application> tag, along side the existing <activity> tags -->
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="com.example.xhale" android:host="callback" />
        </intent-filter>
    </activity>
Axistro
21 Jul, 2025, 13:57

What is the scheme you are using?

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