firebase / flutterfire

đŸ”„ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.68k stars 3.97k forks source link

🐛 [FIREBASE_AUTH] The message field of `FirebaseAuthException` contains broken JSON when receiving `BLOCKING_FUNCTION_ERROR_RESPONSE` #11532

Closed Isti01 closed 9 months ago

Isti01 commented 1 year ago

Bug report

When a blocking function rejects the login the json payload has unexpected characters.

This is the error message of the PlatformException: An internal error has occurred. [ BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]

This is the message field of the FirebaseAuthException: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]

Steps to reproduce

Steps to reproduce the behavior:

  1. Set up a Firebase Auth with a blocking function that rejects all logins and sends a JSON payload back.
  2. Sign in with Firebase Auth
    
    final AuthCredential credential = /* get an auth credential from your provider of choice */;

await FirebaseAuth.instance.signInWithCredential(credential);


### Expected behavior

Have a valid JSON object as the message of the `FirebaseAuthException` that is thrown.

This would be the valid output for the former input:
`{"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}}`

---

### Flutter doctor

Run `flutter doctor` and paste the output below:

<details><summary>Click To Expand</summary>

Doctor summary (to see all details, run flutter doctor -v): Flutter (Channel stable, 3.13.1, on Microsoft Windows [Version 10.0.22621.2134], locale hu-HU) Windows Version (Installed version of Windows is version 10 or higher) Android toolchain - develop for Android devices (Android SDK version 34.0.0) Chrome - develop for the web Visual Studio - develop Windows apps (Visual Studio Community 2022 17.3.6) Android Studio (version 2022.3) IntelliJ IDEA Ultimate Edition (version 2023.2) VS Code (version 1.79.2) Connected device (4 available) Network resources

No issues found!


</details>

---

### Flutter dependencies

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

<details><summary>Click To Expand</summary>

Dart SDK 3.1.0 Flutter SDK 3.13.1 my_app 1.0.0+1

dependencies:

dev dependencies:

transitive dependencies:


russellwheatley commented 1 year ago

I'm not sure on how you can block sign in from a cloud function if signing in via the SDK. Could you provide more details on how you achieved this? The way I can think it can be achieved is with custom claims, but you seem to intimate it can be stopped at the cloud function and the SDK reacts.

Isti01 commented 1 year ago

We did it by using the beforeUserSignedIn event here is the documentation.

And throwing an error inside the function body, like this:

exports[functionName] = beforeUserSignedIn({region: "europe-west1"}, async (event) => {
  throw new HttpsError("failed-precondition", "", "User exists but not found in firestore");
}
Isti01 commented 1 year ago

I submitted a PR with the fix for the issue, https://github.com/firebase/flutterfire/pull/11533

The e2e tests timed out, I hope it's not a problem