Skip to content

signInWithWebUI throws UrlLauncherException instead of UserCancelledException when running on release mode #6395

Description

@nikorehnback

Description

When launching SSO login with Android and returning back UrlLauncherException gets thrown by Amplify Auth instead of UserCancelledException.

This happens only when running on release mode with --release flag.

Issue does not reproduce when running with debug or profile mode.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

Below is minimal sample Flutter code to reproduce the issue.

AndroidManifest.xml

<queries>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="text/plain" />
    </intent>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.support.customtabs.action.CustomTabsService" />
    </intent>
</queries>

main.dart

import 'dart:convert';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _initialized = false;

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

    _init();
  }

  void _init() async {
    final amplifyConfig = {
      "auth": {
        "plugins": {
          "awsCognitoAuthPlugin": {
            "IdentityManager": {"Default": {}},
            "CredentialsProvider": {
              "CognitoIdentity": {
                "Default": {"PoolId": 'PoolId', "Region": 'Region'},
              },
            },
            "CognitoUserPool": {
              "Default": {
                "PoolId": 'PoolId',
                "AppClientId": 'AppClientId',
                "Region": 'Region',
              },
            },
            "Auth": {
              "Default": {
                "authenticationFlowType": "USER_SRP_AUTH",
                "OAuth": {
                  "WebDomain": ' WebDomain',
                  "AppClientId": 'AppClientId',
                  "SignInRedirectURI": 'SignInRedirectURI',
                  "SignOutRedirectURI": 'SignOutRedirectURI',
                  "ResponseType": "code",
                  "Scopes": [
                    "openid",
                    "email",
                    "profile",
                    "aws.cognito.signin.user.admin",
                  ],
                },
              },
            },
          },
        },
      },
    };

    final authPlugin = AmplifyAuthCognito();
    await Amplify.addPlugin(authPlugin);

    try {
      await Amplify.configure(jsonEncode(amplifyConfig));
      setState(() {
        _initialized = true;
      });
    } catch (e) {
      // no-op
    }
  }

  void _signInSSO() async {
    try {
      final result = await Amplify.Auth.signInWithWebUI(
        provider: AuthProvider.google,
      );
      if (result.isSignedIn) {
        // Handle successful sign-in
      }
    } on UserCancelledException {
      if (mounted) {
        const snackBar = SnackBar(content: Text('User cancelled'));
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    } catch (e) {
      if (mounted) {
        final snackBar = SnackBar(content: Text('Error occurred $e'));
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            if (!_initialized)
              ElevatedButton(onPressed: _init, child: Text("Init"))
            else
              ElevatedButton(
                onPressed: _signInSSO,
                child: const Text('Sign in with Google'),
              ),
          ],
        ),
      ),
    );
  }
}

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.35.3

Amplify Flutter Version

2.6.5

Deployment Method

Custom Pipeline

Schema

Metadata

Metadata

Assignees

No one assigned

    Labels

    authIssues related to the Auth CategorybugSomething is not working; the issue has reproducible steps and has been reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions