Skip to content

🐛 [firebase_auth] [firebase_auth/invalid-client-id] Client does not match API key #11082

@Lazizbek97

Description

@Lazizbek97

Bug report

Describe the bug
I have integrated firebase to flutter app using flutterfire cli. I have enabled only phone auth, but when i tried to login with phone numer it's returning error. like: [firebase_auth/invalid-client-id] Client does not match API key (IOS)
i added CFBundleURLTypes to info.plist
i enabled push notifications
i enabled background modes
i uploaded APNs key to firebase console
but nothing helped.

Here is how i use phone auth using cubit:

class AuthCubit extends Cubit<AuthState> {
  AuthCubit() : super(AuthState());

  FirebaseAuth auth = FirebaseAuth.instance;
  FirebaseFirestore firestore = FirebaseFirestore.instance;

  signUpWIthPhoneNumber(
      String phoneNumber, String userName, BuildContext context) async {
    emit(state.copyWith(
        phoneNumber: phoneNumber,
        userName: userName.isEmpty ? state.userName : userName,
        isLoading: true));
    await auth.verifyPhoneNumber(
      phoneNumber: phoneNumber,
      verificationCompleted: (PhoneAuthCredential credential) async {
        // Sign the user in (or link) with the auto-generated credential
        UserCredential userCredential =
            await auth.signInWithCredential(credential);
        emit(state.copyWith(userCredential: userCredential, isLoading: false));
      },
      timeout: Duration(seconds: 60),
      codeAutoRetrievalTimeout: (String verificationId) {},
      codeSent: (String verificationId, int? forceResendingToken) async {
        // Create a PhoneAuthCredential with the code
        emit(state.copyWith(verificationId: verificationId, isLoading: false));
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => CodeVerificationPage(),
          ),
        );
      },
      verificationFailed: (FirebaseAuthException error) {
        emit(state.copyWith(isLoading: false));
        print(error);
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('$error'),
          ),
        );
      },
    );
  }

  signInWithCridentials(String sms) async {
    PhoneAuthCredential _credential = PhoneAuthProvider.credential(
        verificationId: state.verificationId, smsCode: sms);
    UserCredential userCredential =
        await auth.signInWithCredential(_credential);
    await updateUsername();
    createUserInFirestore();
    emit(state.copyWith(userCredential: userCredential));
  }

  updateUsername() async {
    final user = auth.currentUser;
    await user?.updateDisplayName(
        (state.userName.isEmpty) ? 'New User' : state.userName);
  }

  logout() async {
    auth.signOut();
    emit(state.copyWith(userCredential: null));
  }

  createUserInFirestore() async {
    User? user = auth.currentUser;
    DocumentReference<Map<String, dynamic>> firestoreUsers =
        firestore.collection('users').doc(user?.uid);
    var aUser = await firestoreUsers.get();
    if (!aUser.exists) {
      await firestore.collection('users').doc(user?.phoneNumber).set({
        'username': user?.displayName ?? state.userName,
        'createdAt': Timestamp.now().millisecondsSinceEpoch,
        'phoneNumber': user?.phoneNumber,
        'auth_token': user?.uid,
      });
    } else {
      print('This user already sigined in');
    }
  }

  resendCode(BuildContext context) async {
    await auth.verifyPhoneNumber(
      phoneNumber: state.phoneNumber,
      verificationCompleted: (PhoneAuthCredential credential) async {
        // Sign the user in (or link) with the auto-generated credential
        UserCredential userCredential =
            await auth.signInWithCredential(credential);
        emit(state.copyWith(userCredential: userCredential, isLoading: false));
      },
      timeout: Duration(seconds: 60),
      codeAutoRetrievalTimeout: (String verificationId) {},
      codeSent: (String verificationId, int? forceResendingToken) async {
        // Create a PhoneAuthCredential with the code
        emit(state.copyWith(verificationId: verificationId, isLoading: false));
      },
      verificationFailed: (FirebaseAuthException error) {
        emit(state.copyWith(isLoading: false));
        print(error);
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('$error'),
          ),
        );
      },
    );
  }

  setNewCountry(String dialCode, String initialCountry) {
    emit(state.copyWith(dialCode: dialCode, initialCountry: initialCountry));
  }
}

Expected behavior

It should login with phone numbers

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.0, on macOS 13.3.1 22E772610a darwin-arm64, locale en-UZ)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.71.2)
[✓] Connected device (3 available)
[✓] Network resources

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 3.0.0
Flutter SDK 3.10.0
quiz_app 1.0.0+1

dependencies:
- awesome_dialog 3.0.2 [flutter rive]
- cloud_firestore_odm 1.0.0-dev.45 [cloud_firestore flutter json_annotation meta]
- cloud_functions 4.0.7 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter]
- country_list_pick 1.0.1+6 [flutter]
- cupertino_icons 1.0.5
- firebase_auth 4.2.5 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 2.4.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_storage 11.0.10 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- flutter 0.0.0 [characters collection js material_color_utilities meta vector_math sky_engine]
- flutter_bloc 8.1.1 [flutter bloc provider]
- flutter_screenutil 5.8.4 [flutter]
- flutter_svg 2.0.0+1 [flutter vector_graphics vector_graphics_codec vector_graphics_compiler]
- font_awesome_flutter 10.3.0 [flutter]
- freezed_annotation 2.2.0 [collection json_annotation meta]
- hive 2.2.3 [meta crypto]
- hive_flutter 1.1.0 [flutter hive path_provider path]
- http 0.13.5 [async http_parser meta path]
- image_picker 0.8.6+1 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- intl 0.17.0 [clock path]
- json_annotation 4.8.0 [meta]
- pinput 2.2.31 [flutter smart_auth universal_platform]
- provider 6.0.5 [collection flutter nested]
- syncfusion_flutter_sliders 20.4.48 [flutter intl syncfusion_flutter_core]

dev dependencies:
- build_runner 2.3.3 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection js matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]
- freezed 2.3.2 [analyzer build build_config collection meta source_gen freezed_annotation json_annotation]
- hive_generator 2.0.0 [build source_gen hive analyzer source_helper]
- json_serializable 6.6.1 [analyzer async build build_config collection json_annotation meta path pub_semver pubspec_parse source_gen source_helper]

transitive dependencies:
- _fe_analyzer_shared 52.0.0 [meta]
- _flutterfire_internals 1.0.12 [collection firebase_core firebase_core_platform_interface flutter meta]
- analyzer 5.4.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml]
- args 2.3.2
- async 2.11.0 [collection meta]
- bloc 8.1.0 [meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- build 2.3.1 [analyzer async convert crypto glob logging meta path]
- build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml]
- build_daemon 3.1.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel]
- build_resolvers 2.1.0 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml]
- build_runner_core 7.2.7 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml]
- built_collection 5.1.1
- built_value 8.4.3 [built_collection collection fixnum meta]
- characters 1.3.0
- checked_yaml 2.0.2 [json_annotation source_span yaml]
- clock 1.1.1
- cloud_firestore 4.3.1 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cloud_firestore_platform_interface 5.10.1 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.2.1 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- cloud_functions_platform_interface 5.1.26 [firebase_core flutter meta plugin_platform_interface]
- cloud_functions_web 4.3.15 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js]
- code_builder 4.4.0 [built_collection built_value collection matcher meta]
- collection 1.17.1
- convert 3.1.1 [typed_data]
- cross_file 0.3.3+4 [js meta]
- crypto 3.0.2 [typed_data]
- dart_style 2.2.4 [analyzer args path pub_semver source_span]
- fake_async 1.3.1 [clock collection]
- ffi 2.0.1
- file 6.1.4 [meta path]
- firebase_auth_platform_interface 6.11.7 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.2.4 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta]
- firebase_core_platform_interface 4.5.2 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_storage_platform_interface 4.1.26 [collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.3.19 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- fixnum 1.1.0
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- frontend_server_client 3.2.0 [async path]
- glob 2.1.1 [async collection file path string_scanner]
- graphs 2.2.0 [collection]
- http_multi_server 3.2.1 [async]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- image_picker_android 0.8.5+5 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 2.1.10 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_ios 0.8.6+6 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.2 [cross_file flutter http plugin_platform_interface]
- io 1.0.4 [meta path string_scanner]
- js 0.6.7 [meta]
- logging 1.1.1
- matcher 0.12.15 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.2.0
- meta 1.9.1
- mime 1.0.4
- nested 1.0.0 [flutter]
- package_config 2.1.0 [path]
- path 1.8.3
- path_parsing 1.0.1 [vector_math meta]
- path_provider 2.0.12 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows]
- path_provider_android 2.0.22 [flutter path_provider_platform_interface]
- path_provider_foundation 2.1.1 [flutter path_provider_platform_interface]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.0.5 [flutter platform plugin_platform_interface]
- path_provider_windows 2.1.3 [ffi flutter path path_provider_platform_interface win32]
- petitparser 5.1.0 [meta]
- platform 3.1.0
- plugin_platform_interface 2.1.3 [meta]
- pool 1.5.1 [async stack_trace]
- process 4.2.4 [file path platform]
- pub_semver 2.1.3 [collection meta]
- pubspec_parse 1.2.1 [checked_yaml collection json_annotation pub_semver yaml]
- rive 0.9.1 [collection flutter graphs http meta]
- shelf 1.4.0 [async collection http_parser path stack_trace stream_channel]
- shelf_web_socket 1.0.3 [shelf stream_channel web_socket_channel]
- sky_engine 0.0.99
- smart_auth 1.0.8 [flutter flutter_web_plugins]
- source_gen 1.2.7 [analyzer async build dart_style glob path source_span yaml]
- source_helper 1.3.3 [analyzer collection source_gen]
- source_span 1.9.1 [collection path term_glyph]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- stream_transform 2.1.0
- string_scanner 1.2.0 [source_span]
- syncfusion_flutter_core 20.4.48 [vector_math flutter]
- term_glyph 1.2.1
- test_api 0.5.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timing 1.0.1 [json_annotation]
- typed_data 1.3.1 [collection]
- universal_platform 1.0.0+1
- vector_graphics 1.0.1 [flutter vector_graphics_codec]
- vector_graphics_codec 1.0.1
- vector_graphics_compiler 1.0.1 [args meta path_parsing xml vector_graphics_codec]
- vector_math 2.1.4
- watcher 1.0.2 [async path]
- web_socket_channel 2.3.0 [async crypto stream_channel]
- win32 3.1.3 [ffi]
- xdg_directories 0.2.0+3 [meta path process]
- xml 6.2.2 [collection meta petitparser]
- yaml 3.1.1 [collection source_span string_scanner]

Metadata

Metadata

Assignees

No one assigned

    Labels

    resolution: userThis was a user issue, e.g. invalid configuration or code.type: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions