aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.3k stars 239 forks source link

Error message is unclear when oauth is missing from config #4922

Closed sisygoboom closed 1 day ago

sisygoboom commented 1 month ago

Description

Full error:

iOS error:

flutter: Successfully configured
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No instance found for CognitoOAuthConfig
package:amplify_core/src/state_machine/dependency_manager.dart 69:7                       DependencyManager.expect
package:amplify_core/src/state_machine/dependency_manager.dart 148:22                     _ScopedDependencyManager.expect
package:amplify_core/src/state_machine/state_machine.dart 146:26                          StateMachineManager.expect
package:amplify_core/src/state_machine/state_machine.dart 410:60                          StateMachine.expect
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 34:37   HostedUiStateMachine._config
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 35:42   HostedUiStateMachine._keys
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 119:12  HostedUiStateMachine.onSignIn
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_ma<…>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No instance found for CognitoOAuthConfig
package:amplify_core/src/state_machine/dependency_manager.dart 69:7                       DependencyManager.expect
package:amplify_core/src/state_machine/dependency_manager.dart 148:22                     _ScopedDependencyManager.expect
package:amplify_core/src/state_machine/state_machine.dart 146:26                          StateMachineManager.expect
package:amplify_core/src/state_machine/state_machine.dart 410:60                          StateMachine.expect
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 34:37   HostedUiStateMachine._config
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 35:42   HostedUiStateMachine._keys
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 119:12  HostedUiStateMachine.onSignIn
package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_ma<…>

Android error:

E/flutter ( 5553): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No instance found for CognitoOAuthConfig
E/flutter ( 5553): package:amplify_core/src/state_machine/dependency_manager.dart 69:7                       DependencyManager.expect
E/flutter ( 5553): package:amplify_core/src/state_machine/dependency_manager.dart 148:22                     _ScopedDependencyManager.expect
E/flutter ( 5553): package:amplify_core/src/state_machine/state_machine.dart 146:26                          StateMachineManager.expect
E/flutter ( 5553): package:amplify_core/src/state_machine/state_machine.dart 410:60                          StateMachine.expect
E/flutter ( 5553): package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 34:37   HostedUiStateMachine._config
E/flutter ( 5553): package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 35:42   HostedUiStateMachine._keys
E/flutter ( 5553): package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 119:12  HostedUiStateMachine.onSignIn
E/flutter ( 5553): package:amplify_auth_cognito_dart/src/state/machines/hosted_ui_state_machine.dart 57:15   HostedUiStateMachine.resolve
E/flutter ( 5553): package:amplify_core/src/state_machine/state_machine.dart 262:59                          StateMachine._listenForEvents.<fn>.<fn>
E/flutter ( 5553): dart:async/future.dart 422:39                                                             new Future.delayed.<fn>
E/flutter ( 5553): dart:async/zone.dart 1391:47                                                              _rootRun
E/flutter ( 5553): dart:async/zone.dart 1301:19                                                              _CustomZone.run
E/flutter ( 5553): dart:async/zone.dart 1209:7                                                               _CustomZone.runGuarded
E/flutter ( 5553): dart:async/zone.dart 1249:23                                                              _CustomZone.bindCallbackGuarded.<fn>
E/flutter ( 5553): dart:async/zone.dart 1399:13                                                              _rootRun
E/flutter ( 5553): dart:async/zone.dart 1301:19                                                              _CustomZone.run
E/flutter ( 5553): dart:async/zone.dart 1233:23                                                              _CustomZone.bindCallback.<fn>
E/flutter ( 5553): dart:async-patch/timer_patch.dart 18:15                                                   Timer._createTimer.<fn>
E/flutter ( 5553): dart:isolate-patch/timer_impl.dart 398:19                                                 _Timer._runTimers
E/flutter ( 5553): dart:isolate-patch/timer_impl.dart 429:5                                                  _Timer._handleMessage
E/flutter ( 5553): dart:isolate-patch/isolate_patch.dart 184:12                                              _RawReceivePort._handleMessage
E/flutter ( 5553): ===== asynchronous gap ===========================
E/flutter ( 5553): package:amplify_core/src/state_machine/event.dart 39:47                                   new EventCompleter
E/flutter ( 5553): package:amplify_core/src/state_machine/state_machine.dart 157:23                          StateMachineManager.accept
E/flutter ( 5553): package:amplify_auth_cognito_dart/src/auth_plugin_impl.dart 329:10                        AmplifyAuthCognitoDart.signInWithWebUI
E/flutter ( 5553): package:amplify_core/src/category/amplify_auth_category.dart 1052:29                      AuthCategory.signInWithWebUI.<fn>
E/flutter ( 5553): dart:async/zone.dart 1399:13                                                              _rootRun
E/flutter ( 5553): dart:async/zone.dart 1301:19                                                              _CustomZone.run
E/flutter ( 5553): dart:async/zone.dart 1804:10                                                              _runZoned
E/flutter ( 5553): dart:async/zone.dart 1747:10                                                              runZoned
E/flutter ( 5553): package:amplify_core/src/http/amplify_category_method.dart 147:10                         identifyCall
E/flutter ( 5553): package:amplify_core/src/category/amplify_auth_category.dart 1050:7                       AuthCategory.signInWithWebUI
E/flutter ( 5553): package:datingapp/widgets/login/socialLogin.dart 101:30                                   SocialLoginButtons.signInWithProvider
E/flutter ( 5553): package:datingapp/widgets/login/socialLogin.dart 53:15                                    SocialLoginButtons.loginButton.<fn>
E/flutter ( 5553): package:flutter/src/material/ink_well.dart 1183:21                                        _InkResponseState.handleTap
E/flutter ( 5553): package:flutter/src/gestures/recognizer.dart 315:24                                       GestureRecognizer.invokeCallback
E/flutter ( 5553): package:flutter/src/gestures/tap.dart 652:11                                              TapGestureRecognizer.handleTapUp
E/flutter ( 5553): package:flutter/src/gestures/tap.dart 309:5                                               BaseTapGestureRecognizer._checkUp
E/flutter ( 5553): package:flutter/src/gestures/tap.dart 242:7                                               BaseTapGestureRecognizer.handlePrimaryPointer
E/flutter ( 5553): package:flutter/src/gestures/recognizer.dart 670:9                                        PrimaryPointerGestureRecognizer.handleEvent
E/flutter ( 5553): package:flutter/src/gestures/pointer_router.dart 98:12                                    PointerRouter._dispatch
E/flutter ( 5553): package:flutter/src/gestures/pointer_router.dart 143:9                                    PointerRouter._dispatchEventToRoutes.<fn>
E/flutter ( 5553): dart:collection-patch/compact_hash.dart 633:13                                            _LinkedHashMapMixin.forEach
E/flutter ( 5553): package:flutter/src/gestures/pointer_router.dart 141:18                                   PointerRouter._dispatchEventToRoutes
E/flutter ( 5553): package:flutter/src/gestures/pointer_router.dart 127:7                                    PointerRouter.route
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 495:19                                          GestureBinding.handleEvent
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 475:22                                          GestureBinding.dispatchEvent
E/flutter ( 5553): package:flutter/src/rendering/binding.dart 430:11                                         RendererBinding.dispatchEvent
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 420:7                                           GestureBinding._handlePointerEventImmediately
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 383:5                                           GestureBinding.handlePointerEvent
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 330:7                                           GestureBinding._flushPointerEventQueue
E/flutter ( 5553): package:flutter/src/gestures/binding.dart 299:9                                           GestureBinding._handlePointerDataPacket
E/flutter ( 5553): dart:ui/hooks.dart 328:13                                                                 _invoke1
E/flutter ( 5553): dart:ui/platform_dispatcher.dart 429:7                                                    PlatformDispatcher._dispatchPointerDataPacket
E/flutter ( 5553): dart:ui/hooks.dart 262:31                                                                 _dispatchPointerDataPacket
E/flutter ( 5553):

Categories

Steps to Reproduce

  1. configure social login with Facebook and google, have email login disabled, mobile login enabled
  2. configure the authenticator with a custom UI:
    return Authenticator(
      authenticatorBuilder: (BuildContext context, AuthenticatorState state) {
        return SocialLoginButtons();
      },
  3. when social login button is clicked, execute login with webui:
        case LoginPlatform.facebook:
          await Amplify.Auth.signInWithWebUI(provider: AuthProvider.facebook);
          break;
        case LoginPlatform.google:
          await Amplify.Auth.signInWithWebUI(provider: AuthProvider.google);
          break;
  4. error should surface with no web browser open

Screenshots

No response

Platforms

Flutter Version

3.22.0 & 3.19.6

Amplify Flutter Version

2.0.0

Deployment Method

AWS CDK

Schema

No response

Jordan-Nelson commented 1 month ago

Hello @sisygoboom. Can you share your Amplify Configuration? You can remove any sensitive or app specific information.

sisygoboom commented 1 month ago

Auth construct:

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: {
    phone: true,
    externalProviders: {
      facebook: {
        clientId: secret('FACEBOOK_APP_ID'),
        clientSecret: secret('FACEBOOK_APP_SECRET'),
        scopes: ['email', 'public_profile'],
      },
      google: {
        clientId: secret('GOOGLE_CLIENT_ID'),
        clientSecret: secret('GOOGLE_CLIENT_SECRET'),
        scopes: ['email', 'profile'],
      },
      callbackUrls: ['paritae://callback'], // Ensure these match your actual callback URLs
      logoutUrls: ['paritae://logout'], // Ensure these match your actual logout URLs
    },    
  },
  userAttributes: {
    email: {
      required: true,
      mutable: true
    },
    phoneNumber: {
      required: false,
      mutable: true
    },
    profilePicture: {
      required: false,
      mutable: true,
    }
  }
});

amplify config:

const amplifyConfig = '''{
  "UserAgent": "@aws-amplify/client-config/1.0.2",
  "Version": "1.0",
  "auth": {
    "plugins": {
      "awsCognitoAuthPlugin": {
        "UserAgent": "@aws-amplify/client-config/1.0.2",
        "Version": "1.0",
        "CognitoUserPool": {
          "Default": {
            "PoolId": "REDACTED",
            "AppClientId": "REDACTED",
            "Region": "REDACTED"
          }
        },
        "CredentialsProvider": {
          "CognitoIdentity": {
            "Default": {
              "PoolId": "REDACTED",
              "Region": "REDACTED"
            }
          }
        },
        "Auth": {
          "Default": {
            "authenticationFlowType": "USER_SRP_AUTH",
            "passwordProtectionSettings": {
              "passwordPolicyMinLength": 8,
              "passwordPolicyCharacters": [
                "REQUIRES_NUMBERS",
                "REQUIRES_LOWERCASE",
                "REQUIRES_UPPERCASE",
                "REQUIRES_SYMBOLS"
              ]
            },
            "signupAttributes": [
              "EMAIL"
            ],
            "usernameAttributes": [
              "PHONE_NUMBER"
            ],
            "verificationMechanisms": [
              "PHONE_NUMBER"
            ]
          }
        },
        "AppSync": {
          "Default": {
            "ApiUrl": "REDACTED",
            "Region": "REDACTED",
            "AuthMode": "REDACTED",
            "ClientDatabasePrefix": "REDACTED"
          },
          "data_AMAZON_COGNITO_USER_POOLS": {
            "ApiUrl": "REDACTED",
            "Region": "REDACTED",
            "AuthMode": "REDACTED",
            "ClientDatabasePrefix": "REDACTED"
          }
        }
      }
    }
  },
  "api": {
    "plugins": {
      "awsAPIPlugin": {
        "data": {
          "endpointType": "GraphQL",
          "endpoint": "REDACTED",
          "region": "REDACTED",
          "authorizationType": "REDACTED"
        }
      }
    }
  }
}''';

Command used:

npx ampx sandbox --outputs-format dart --outputs-out-dir lib --outputs-version 0

Jordan-Nelson commented 1 month ago

@sisygoboom Thank you. It looks like the issue is that the oAuth info isn't added to your config. We have an issue open for this already: https://github.com/aws-amplify/amplify-backend/issues/1551

I am going to keep this issue open because Amplify Flutter should throw a better error message in this scenario.

NikaHsn commented 1 day ago

This was resolved in version 2.2.0. If you are still facing issue after upgrading please let us know. Thanks.