Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_auth]: error.credential is null on credential-already-in-use exception during apple sign in in web #12771

Closed
1 task done
BogsBalangat opened this issue May 13, 2024 · 3 comments · Fixed by #12780
Assignees
Labels
Needs Attention This issue needs maintainer attention. platform: web Issues / PRs which are specifically for web. plugin: auth resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working

Comments

@BogsBalangat
Copy link

BogsBalangat commented May 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Auth

Which platforms are affected?

Web

Description

Firebase auth in web returns a null credential value on a "credential-already-in-use" FirebaseAuthException during apple sign in.
This prevents the web app from being able to recover and continue with the sign in flow.

Notes:
This issue only happens on flutter web.
This does not happen during Google Sign in on flutter web.
It is working as expected (error.credential is not null) in flutter mobile (both android & ios)

Reproducing the issue

Make sure that you already have a firebase user linked to an apple account.

Then run the following code.
A new anonymous user will be created and make sure to sign in to the same apple account.

Here is a sample code:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(MaterialApp(
    home: const App(),
  ));
}

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  final auth = FirebaseAuth.instance;
  @override
  void initState() {
    super.initState();
    _createAnonymousAccount();
  }

  Future<void> _createAnonymousAccount() async {
    if (auth.currentUser == null) {
      await auth.signInAnonymously();
    }
  }

  Future<void> _signIn() async {
    try {
      await auth.currentUser!.linkWithPopup(AppleAuthProvider());
      print('Link successful');
    } on FirebaseAuthException catch (e) {
      if (e.code == 'credential-already-in-use') {
        final cred = e.credential!;
        await auth.signInWithCredential(cred);
        print('Sign in successful');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: _signIn,
          child: const Text('Sign in'),
        ),
      ),
    );
  }
}

Firebase Core version

2.30.1

Flutter Version

3.19.5

Relevant Log Output

Error: Unexpected null value.
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3       throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 806:18  nullCheck
packages/test/main.dart 44:34                                               _signIn
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 60:31                <fn>
dart-sdk/lib/async/zone.dart 1666:54                                              runBinary
dart-sdk/lib/async/future_impl.dart 177:22                                        handleError
dart-sdk/lib/async/future_impl.dart 850:46                                        handleError
dart-sdk/lib/async/future_impl.dart 871:13                                        _propagateToListeners
dart-sdk/lib/async/future_impl.dart 651:5                                         [_completeError]
dart-sdk/lib/async/future_impl.dart 737:7                                         callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                                  _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                                   _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7                <fn>

Flutter dependencies

Expand Flutter dependencies snippet
Dart SDK 3.3.3
Flutter SDK 3.19.5
colabrador 1.4.0+92

dependencies:
- cupertino_icons 1.0.6
- firebase_auth 4.19.4 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 2.30.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- localization 2.1.0 [flutter]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service]

transitive dependencies:
- _flutterfire_internals 1.3.32 [collection firebase_core firebase_core_platform_interface flutter meta]
- async 2.11.0 [collection meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- characters 1.3.0
- clock 1.1.1
- collection 1.18.0
- fake_async 1.3.1 [clock collection]
- firebase_auth_platform_interface 7.2.5 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.11.4 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser meta web]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.16.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web]
- flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- intl 0.18.1 [clock meta path]
- leak_tracker 10.0.0 [clock collection meta path vm_service]
- leak_tracker_flutter_testing 2.0.1 [flutter leak_tracker leak_tracker_testing matcher meta]
- leak_tracker_testing 2.0.1 [leak_tracker matcher meta]
- matcher 0.12.16+1 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.8.0 [collection]
- meta 1.11.0
- path 1.9.0
- plugin_platform_interface 2.1.8 [meta]
- sky_engine 0.0.99
- source_span 1.10.0 [collection path term_glyph]
- stack_trace 1.11.1 [path]
- stream_channel 2.1.2 [async]
- string_scanner 1.2.0 [source_span]
- term_glyph 1.2.1
- test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph]
- typed_data 1.3.2 [collection]
- vector_math 2.1.4
- vm_service 13.0.0
- web 0.5.1

Additional context and comments

Here is a screenshot showing actual values of the FirebaseAutghException.
Screenshot 2024-05-13 at 10 13 40 AM

*Shows that credential is null

@BogsBalangat BogsBalangat added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels May 13, 2024
@TarekkMA TarekkMA added plugin: auth platform: web Issues / PRs which are specifically for web. labels May 13, 2024
@TarekkMA TarekkMA self-assigned this May 13, 2024
@TarekkMA
Copy link
Contributor

Hello @BogsBalangat, thank you for reporting this issue. I've just tested the Apple login on the web, and it works without any issues.

Could you please provide a minimal but complete example, or detail the steps that trigger this issue? Additionally, you might try integrating your Apple Sign-In and Firebase with the example app found here to test if the issue persists.

@TarekkMA TarekkMA added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels May 13, 2024
@BogsBalangat
Copy link
Author

Hi @TarekkMA, thank you for looking into this. I updated the repro steps and included code to run. The important condition here is that there should already be an existing firebase user linked to the apple account. That is why when the new anonymous user attempts to link the same apple account, firebase auth will throw an credential-already-in-use exception. In this case, the exception should have a non-null credential value which I can use to sign in instead..

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels May 13, 2024
@TarekkMA
Copy link
Contributor

Thank you, @BogsBalangat, for updating the steps required to reproduce the issue. I have been able to reproduce it and created a pure JS example where this issue does not occur. You can view it here: JS Example.

We will look into fixing this issue.

@russellwheatley

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Attention This issue needs maintainer attention. platform: web Issues / PRs which are specifically for web. plugin: auth resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working
Projects
None yet
3 participants