I’m building a flutter app and have been struggling to add amplify authentication to my project work. So I started a fresh project and followed this guide, in order to isolate the problem. As far as I can tell I’ve followed the guide exactly; I did the “Set up Amplify Auth” section and can create users and login with username/password. Then I set up google authentication by following the External Identity Providers section, and then the sign-in with web ui section, and was able to get everything working fine in the emulator.
But if I run it on my phone connected to my computer, it doesn’t work. I can click on the “Sign in with Google” button, go through the authentication process with google, but instead of redirecting back to the main page, i get presented with the login page again. The username and password login works normally.
I don’t see any errors in the debug console, and I don’t really know how else to debug it. So I’ve put all the code here:
I’m trying to use gen 2 amplify, but I’ve seen the same behavior in gen 1.
Relevant bits of configuration:
pubspec.yaml
name: amplify_test
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0
environment:
sdk: ^3.5.0
dependencies:
flutter:
sdk: flutter
amplify_flutter: ^2.4.1
amplify_auth_cognito: ^2.4.1
amplify_authenticator: ^2.1.3
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
flutter:
uses-material-design: true
AndroidManifext.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="amplify_test"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
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>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
</queries>
</manifest>
resource.ts
import { defineAuth, secret } from '@aws-amplify/backend';
/**
* Define and configure your auth resource
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
*/
export const auth = defineAuth({
loginWith: {
email: true,
externalProviders: {
google: {
clientId: secret('GOOGLE_CLIENT_ID'),
clientSecret: secret('GOOGLE_CLIENT_SECRET'),
scopes: ['email'],
attributeMapping: {
email: 'email'
}
},
callbackUrls: ["myapp://callback/"],
logoutUrls: ["myapp://signout/"],
},
},
});
main.dart
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_authenticator/amplify_authenticator.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';
import 'amplify_outputs.dart';
Future<void> main() async {
try {
WidgetsFlutterBinding.ensureInitialized();
await _configureAmplify();
runApp(const MyApp());
} on AmplifyException catch (e) {
runApp(Text("Error configuring Amplify: ${e.message}"));
}
}
Future<void> _configureAmplify() async {
try {
await Amplify.addPlugin(AmplifyAuthCognito());
await Amplify.configure(amplifyConfig);
safePrint('Successfully configured');
} on Exception catch (e) {
safePrint('Error configuring Amplify: $e');
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return Authenticator(
child: MaterialApp(
builder: Authenticator.builder(),
home: const Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SignOutButton(),
],
),
),
),
),
);
}
}
I tried Amplify gen 1 and 2, with a few different versions of amplify_authenticator, but it didn’t seem to make any difference.
UPDATE: I had an epiphany; one major difference between the emulator and my physical phone is that I have firefox set as my default browser. Sure enough, when I changed it to chrome, everything worked as expected. That led me to this open issue. So it is a known bug in amplify authenticator. I’m leaving this for future time travelers, and will try to remember to update if it gets fixed.
1