amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

PlatformException - Cognito

Open arealguru opened this issue 6 years ago • 8 comments

Bug Description I'm trying to set up just the AmplifyAuthCognito() plugin, but I'm having difficulty as a PlatformException (most likely a bad amplifyconfiguration.dart was keeping me from getting anywhere. I looked at issues #100 and #153 (#100 was much closer to my situation), and I wasn't able to replicate the fixes from those configurations in mine.

To Reproduce I used the Amplify CLI and the tutorial and I currently have Auth set up; amplify status outputs

| Category | Resource name     | Operation | Provider plugin   |
| -------- | ----------------- | --------- | ----------------- |
| Auth     | <value> | No Change | awscloudformation |

This is what my config looks like within amplifyconfiguration.dart.

const amplifyconfig = ''' {
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "UserAgent": "aws-amplify-cli/0.1.0",
                "Version": "0.1.0",
                "IdentityManager": {
                    "Default": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "<value>",
                            "Region": "us-east-2"
                        }
                    }
                },
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "us-east-2_<value>",
                        "AppClientId": "<value>",
                        "AppClientSecret": "<value>",
                        "Region": "us-east-2"
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH"
                    }
                }
            }
        }
    }
}''';

I'm then running the following test code:

class _MyHomePageState extends State<MyHomePage> {
  bool _amplifyConfigured = false;
  Amplify amplifyInstance = Amplify();

  @override
  initState() {
    super.initState(); 
    _configureAmplify();
  }

  void _configureAmplify() async {
    AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
    amplifyInstance.addPlugin(
      authPlugins: [authPlugin]
    );

    // Once Plugins are added, configure Amplify
    await amplifyInstance.configure(amplifyconfig);
    try {
      setState(() {
        _amplifyConfigured = true;
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: LoginPage(),
    );
  }
}

with my .yaml file including:

dependencies:
  flutter:
    sdk: flutter
  amplify_core: '<1.0.0'
  amplify_auth_cognito: '<1.0.0'
  amplify_analytics_pinpoint: '<1.0.0'

Expected behavior No error, but unfortunately I'm seeing:

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: PlatformException(AmplifyException, Failed to Configure Amplify, The operation couldn’t be completed. (Amplify.ConfigurationError error 0.), null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:582:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:18)
<asynchronous suspension>
#2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
#3      MethodChannelAmplifyCore.configure (package:amplify_core_plugin_interface/method_channel_amplify.dart:25:21)
#4      Amplify.configure (package:amplify_core/amplify_core.dart:71:35)
#5      _MyHomePageState._configureAmplify (package:mobile/main.dart:65:27)
#6      _MyHomePageState.initState (package:mobile/main.dart:55:5)
#7      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
#8      ComponentElement.mount (package:flutter/src/widgets/framework.da<…>

Platform Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply): [] Android [x] iOS

Smartphone:

  • Device: iPhone SE Emulator
  • OS: iOS 14

arealguru avatar Oct 17 '20 08:10 arealguru

@arealguru I'm unable to reproduce this issue, but we are continuing to investigate. We have a couple of follow-up questions:

  1. Is this reproducible on an Android device?
  2. Which versions of the libraries are you actually using? These should be visible in the pubspec.lock file.

haverchuck avatar Oct 23 '20 16:10 haverchuck

Hi, yesterday got the same exception on Android side. Found some mistakes in the amplifyconfig file but iOS didn't show up this exception, which is weird. Our team created the config file manually. The detailed message indicates the user agent has been set, and Oauth related error. If one has the exception above I suggest look at the official config:

The wrong config file:

const amplifyconfig = ''' {
  "UserAgent": "aws-amplify-cli/2.0",
  "Version": "1.0",
  "auth": {
    "plugins": {
      "awsCognitoAuthPlugin": {
        "UserAgent": "aws-amplify-cli/2.0",
        "Version": "0.1.0",
        "IdentityManager": {
          "Default": {}
        },
        "CognitoUserPool": {
          "Default": {
            "PoolId": "ap-northeast-1_RqSQCb4zW",
            "AppClientId": "548cm6u147musag0f1a1ee8i4j",
            "AppClientSecret": "14rjh2knh2tqah8vcuv33n57048oe1qgipf83287tfravfip78s1",
            "Region": "ap-northeast-1"
          }
        },
        "Auth": {
          "Default": {
            "OAuth": {
              "WebDomain": "nodesfly-dev.auth.ap-northeast-1.amazoncognito.com",
              "AppClientId": "548cm6u147musag0f1a1ee8i4j",
              "AppClientSecret": "14rjh2knh2tqah8vcuv33n57048oe1qgipf83287tfravfip78s1",
              "Scopes": [
                "phone",
                "email",
                "openid",
                "profile",
                "aws.cognito.signin.user.admin"
              ]
            },
            "authenticationFlowType": "USER_SRP_AUTH"
          }
        }
      }
    }
  }
}''';

So what I did was:

  1. remove the UserAgent and Version in the awsCognitoAuthPlugin section
  2. remove the whole OAuth block

and all exceptions are gone.

dependencies: amplify_core: '<1.0.0' amplify_auth_cognito: '<1.0.0'

suandplzop avatar Oct 28 '20 02:10 suandplzop

Thanks for the suggestion @scdplzop, I have removed the UserAgent and Version in the awsCognitoAuthPlugin, and I never had the OAuth block in.

It is now working! Had to completely clean all my caches and relaunch the emulator, and it was able to work. Thanks everyone.

Works for both android and iOS.

arealguru avatar Oct 28 '20 07:10 arealguru

I've had this problem for days except it says plugin error 2 instead of 0. I put endless hours into this and finally fixed it by deleting the auth and analytics folders in pods.

Josiah-Turnquist avatar Nov 08 '20 08:11 Josiah-Turnquist

I've had this problem for days except it says plugin error 2 instead of 0. I put endless hours into this and finally fixed it by deleting the auth and analytics folders in pods.

I am getting error 2 ...deleting the folders didn't fix my issue

mo-malaka avatar Dec 28 '20 03:12 mo-malaka

Hi we are taking a look into this issue and actively working on a fix. Currently if you add one of our plugins to pubspec without adding it via the amplify cli as well the above issue will occur.

fjnoyp avatar Feb 23 '21 19:02 fjnoyp

@fjnoyp Is it related to the hotreload function?

SalahAdDin avatar Apr 02 '21 20:04 SalahAdDin

Hey @SalahAdDin @arealguru @jareddplzop @Josiah-Turnquist

I cannot reproduce this issue anymore

Feel free to test using the latest build and let us know if you got any issues

Regards Mo

mo-malaka avatar Sep 01 '21 14:09 mo-malaka