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

feat(firebase_auth): implement support for useEmulator #4263

Merged
merged 3 commits into from
Jan 28, 2021

Conversation

mikehardy
Copy link
Contributor

@mikehardy mikehardy commented Nov 29, 2020

Description

implements FirebaseAuth.useEmulator(string origin)

Review notes:

  • the emulator suite has grown since originally used here, it is easier to consolidate emulator usage in CI, so I have done so in the scripts
  • JS SDK takes string "origin", iOS/Android takes host/port, API uses string for all platforms, splits host/port for ios/android with testing
  • The e2e tests can run against the emulator but a few tests fail. It proves it works though

Future work:

I implemented this for react-native-firebase but similar to caution in my forward-port PR here, I am new to Dart/Flutter/FlutterFire so I need assistance with some of the fundamentals. Be gentle :-)

Please help testing the PR

Add the following to your pubspec.yaml:

dependency_overrides:
  firebase_auth:
    git:
      url: https://github.com/mikehardy/flutterfire.git
      ref: auth-emulator
      path: packages/firebase_auth/firebase_auth
  firebase_auth_web:
    git:
      url: https://github.com/mikehardy/flutterfire.git
      ref: auth-emulator
      path: packages/firebase_auth/firebase_auth_web
  firebase_auth_platform_interface:
    git:
      url: https://github.com/mikehardy/flutterfire.git
      ref: auth-emulator
      path: packages/firebase_auth/firebase_auth_platform_interface

User notes:

Related Issues

#3969

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@mikehardy
Copy link
Contributor Author

Flaky test:

android

[firebase_storage_example]:   updateMetadata
[firebase_storage_example]:      ✔ updates metadata
[firebase_storage_example]:      ✘ errors if metadata update removes existing data
[firebase_storage_example]:        [E]: [firebase_storage/object-not-found] No object exists at the desired reference.

apple


[firebase_storage_example]:   updateMetadata
[firebase_storage_example]:      ✔ updates metadata
[firebase_storage_example]:      ✘ errors if metadata update removes existing data
[firebase_storage_example]:        [E]: Expected: {'action': 'updateMetadata test'}
[firebase_storage_example]:               Actual: {'activity': 'test', 'action': 'updateMetadata test'}

@alejandroaap
Copy link

Please keep this PR in mind and approve it as soon as possible. This functionality is important, the sdks cannot be so outdated, they must have the same functionality as the native ones. Thank you.

@mikehardy
Copy link
Contributor Author

Thanks @alejandroaap for the vote of confidence

To any reviewers, please note I understand this needs collaboration (and associated re-shaping as requested) to land, I'm ready to do that.

In particular I consider this the last part of a series of PRs that should go in first, and as they go in, commits will disappear (via git rebase / re-push to keep it clean + easy to review ultimately) from here

First up is testing to prove things do not break as change comes in: #4331
Next up is some compile fixes for firebase_ml_vision #4330
Then some foundation work to harmonize SDK versions: #4332
A big change that needs collaboration to get to the new SDKs #4249
And finally this can land

It will be a journey to get here in other words @alejandroaap - as each of those PRs may need to be reshaped a bit until accepted - please be patient

@mikehardy
Copy link
Contributor Author

For those following along at home, we are hard at work in the new year
All of the PRs this one depended on have been merged, reducing this to just 2 commits left, and it's in progress

We're almost there

@mikehardy mikehardy force-pushed the auth-emulator branch 6 times, most recently from 3051c60 to 422597e Compare January 12, 2021 23:06
@mikehardy mikehardy marked this pull request as ready for review January 12, 2021 23:43
@Salakar Salakar added plugin: auth type: enhancement New feature or request type: missing-feature A feature that is supported on the underlying Firebase SDK but has not been exposed to Dart API. labels Jan 12, 2021
@mikehardy
Copy link
Contributor Author

Ready to go, I think. The PR description contents have been updated to reflect all relevant information and instructions for users to use the feature in testing.

Copy link
Member

@russellwheatley russellwheatley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, @mikehardy 💪 . I've made a few points that might warrant further attention.

I ran integration tests locally, and the setup was working for me, albeit with a number of tests failures. Some errors are of the same type, so once debugged, it will probably solve a few test failures. Good start though 😄

.github/workflows/firebase_auth.yaml Show resolved Hide resolved
.github/workflows/scripts/firebase.json Show resolved Hide resolved
Copy link
Contributor Author

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replied but I have to explicitly comment as well on mobile github...

Copy link
Member

@russellwheatley russellwheatley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In light of feedback, LGTM 👍

@mikehardy
Copy link
Contributor Author

For those following along - thinking of testing this: This has had a couple rounds of review now and I've just pushed what should be the final version. There are a pile of firebase_auth PRs in progress right this moment, and this is not first in the queue.

So it will sit for a little bit while those land, then I'll ingest the changes here and this should land as is.

Nothing is permanent in software but I don't anticipate anything will change, so it should be good to work with if you want to follow the testing instructions above and use it in your project

Cheers

Copy link
Member

@Salakar Salakar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's shipping time! Thank you for sorting this @mikehardy

LGTM

@Salakar Salakar changed the title feat(firebase_auth, emulator): implement useEmulator feat(firebase_auth): implement support for useEmulator Jan 28, 2021
@Salakar Salakar merged commit e65623d into firebase:master Jan 28, 2021
@Salakar
Copy link
Member

Salakar commented Jan 28, 2021

Available in 0.21.0-1.1.nullsafety.0 onwards.

@mikehardy
Copy link
Contributor Author

Fantastic! Thanks for all the help everyone

@pinkasey
Copy link

pinkasey commented Feb 3, 2021

Thanks for this new feature!
question -
is it possible to port this change to flutter stable channel?
I want to use useEmulator in my integration-test, but I'm currently on Flutter stable channel, and trying to move to Flutter beta channel causes a conflict in my dependencies which I cannot solve
(I've tried to put any in every dependency I have except firebase_auth - still have a conflict).
Is the null-safety really a must for this feature?

@pinkasey
Copy link

pinkasey commented Feb 3, 2021

Alternatively -
do you have an estimate of when can this reach flutter stable channel?

Salakar added a commit that referenced this pull request Feb 3, 2021
@mikehardy mikehardy deleted the auth-emulator branch February 3, 2021 15:13
@mikehardy
Copy link
Contributor Author

@pinkasey looks like you might be in luck! It appears @Salakar is cherry-picking this onto the stable maintenance branch

Salakar added a commit that referenced this pull request Feb 3, 2021
@pinkasey
Copy link

pinkasey commented Feb 3, 2021

@pinkasey looks like you might be in luck! It appears @Salakar is cherry-picking this onto the stable maintenance branch

OMG that's not the reply I expected!
So exciting!!!
Thank you @Salakar !

@slateandchalk
Copy link

slateandchalk commented Feb 6, 2021

@Salakar

Web:
FirebaseAuth.instance.useEmulator('http://localhost:9099');

iOS:
FirebaseAuth.instance.useEmulator('http://10.1.80.63:9099');

both are working fine. but, android, i getting error. PF
FirebaseAuth.instance.useEmulator('http://10.1.80.63:9099');
FirebaseAuth.instance.useEmulator('http://10.0.2.2:9099');

i tried above mentioned two methods.

Error:
W/System ( 9441): Ignoring header X-Firebase-Locale because its value was null.

Physical device is working fine. with FirebaseAuth.instance.useEmulator('http://10.1.80.63:9099');

@mikehardy
Copy link
Contributor Author

on the android emulator you should just use localhost, it will transparently remap to 10.0.2.2 and in my testing while implementing this it was working assuming you had the appropriate network security policy built into the app that allowed insecure local transport (a change in AndroidManifest.xml that is visible in the PR)

@goldcoders
Copy link

I wanna use this on stable version of flutter is this possible?

@Parakoos
Copy link

I too wonder if there are plans to make this available on the stable. If not, can we get advice for what needs to be done do get access to this? Which flutter channel should we be on, and what changes to the pubspec do we need to make?

@mikehardy
Copy link
Contributor Author

If you switch to master, then the instructions at the top will work for you
If you do not, then you will need to somehow get the "no-nullsafety" branch of FlutterFire, which can probably work with stable ? I'm unsure there.

@Parakoos
Copy link

Thanks for the tip in trying the no-nullsafety branch. I did try to switch over to that branch, but if I see it correct, there seems to be some reliance on the null-branch anyway. Here is what I added to pubspec, taken from the above

dependency_overrides:
  firebase_auth:
    git:
      url: https://github.com/FirebaseExtended/flutterfire.git
      ref: no-nullsafety
      path: packages/firebase_auth/firebase_auth
  firebase_auth_web:
    git:
      url: https://github.com/FirebaseExtended/flutterfire.git
      ref: no-nullsafety
      path: packages/firebase_auth/firebase_auth_web
  firebase_auth_platform_interface:
    git:
      url: https://github.com/FirebaseExtended/flutterfire.git
      ref: no-nullsafety
      path: packages/firebase_auth/firebase_auth_platform_interface

And what I get when I run 'flutter pub get'

Package doesn't exist (the pubspec for firebase_auth_platform_interface 3.0.1 from git has version 4.0.0-1.1.nullsafety.1).
pub get failed (server unavailable) -- attempting retry 1 in 1 second...

That is a strange message I think since it somehow seems to refer to to a nullsafety version, but I have to say, I don't see where it does that. I'm not so well-versed in pub and dart to really understand what is going on here, so posting this message in the hope someone might be able to explain it.

For full reference, if I switch out the 'no-nullsafety' to 'master' then I don't get the error message that the package doesn't exist, but rather the error that versions are incompatible.

Also, here is the rest of my dependencies:

environment:
  sdk: ">=2.10.5 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^0.7.0
  firebase_auth: ^0.20.0
  firebase_storage: ^7.0.0
  firebase_ml_vision: ^0.10.0
  cloud_firestore: ^0.16.0

@xuanswe
Copy link

xuanswe commented Feb 14, 2021

@mikehardy @Salakar

It appears @Salakar is cherry-picking this onto the stable maintenance branch

on firebase_auth 0.20.0+1 but still don't see the useEmulator method?

@mikehardy
Copy link
Contributor Author

mikehardy commented Feb 14, 2021

Let's follow the trail:

There are no tags on that commit yet. So I would assume it has not been released.

Looking at the list of commits on the branch, it seems that releases will be tagged and see a commit

https://github.com/FirebaseExtended/flutterfire/commits/no-nullsafety

So I'm 99% sure now it just hasn't been released

Patience, all :-)

@Salakar
Copy link
Member

Salakar commented Feb 18, 2021

The following package versions have been published with this in for the non-nullsafety versions:

firebase_auth_platform_interface      3.1.0           
firebase_auth_web                     0.3.3           
firebase_auth                         0.20.1  

@mikehardy
Copy link
Contributor Author

@Salakar, the hero the world needs right now ;-)

@pxsanghyo
Copy link

I tried it on web but it crashes. Works fine on iOS and Android.

firebase_auth: ^0.20.1
  if (environment == Environment.dev) {
    final host = defaultTargetPlatform == TargetPlatform.android
        ? '10.0.2.2'
        : 'localhost';
    await g<FirebaseAuth>().useEmulator('http://$host:9099');
    g<FirebaseFirestore>().settings = Settings(
      host: '$host:8080',
      sslEnabled: false,
      persistenceEnabled: false,
    );
  }
WARNING: You are using the Auth Emulator, which is intended for local testing only.  Do not use with production
credentials.
Error: [firebase_auth/unknown] An unknown error occurred: TypeError: Cannot read property 'then' of undefined
    at Object.throw_ [as throw] (http://localhost:61210/dart_sdk.js:5331:11)
    at firebase_auth_web.FirebaseAuthWeb.new.useEmulator
    (http://localhost:61210/packages/firebase_auth_web/firebase_auth_web.dart.lib.js:313:21)
    at useEmulator.throw (<anonymous>)
    at http://localhost:61210/dart_sdk.js:39035:38
    at _RootZone.runBinary (http://localhost:61210/dart_sdk.js:38891:58)
    at _FutureListener.thenAwait.handleError (http://localhost:61210/dart_sdk.js:33884:48)
    at handleError (http://localhost:61210/dart_sdk.js:34448:51)
    at Function._propagateToListeners (http://localhost:61210/dart_sdk.js:34474:17)
    at _Future.new.[_completeError] (http://localhost:61210/dart_sdk.js:34320:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:61210/dart_sdk.js:34359:31)
    at Object._microtaskLoop (http://localhost:61210/dart_sdk.js:39173:13)
    at _startMicrotaskLoop (http://localhost:61210/dart_sdk.js:39179:13)
    at http://localhost:61210/dart_sdk.js:34686:9

@pinkasey
Copy link

Awesome!
I'll check it this Tuesday/Thursday on Flutter, and update here.

@mikehardy
Copy link
Contributor Author

mikehardy commented Feb 21, 2021

@pxsanghyo oh no! It's likely this is a bug in the underlying implementation yes, I was able to test android/iOS pretty thoroughly but not web. So, thank you for your report and sorry it gave trouble.

Thanks a bunch for the version + stack trace, that helps.

Your version corresponds to the no-nullsafety branch: https://github.com/FirebaseExtended/flutterfire/commits/no-nullsafety/packages/firebase_auth

And it appears we are on this line: https://github.com/FirebaseExtended/flutterfire/blob/2eb0751269b679ced0c033823d896aa4e8daea82/packages/firebase_auth/firebase_auth_web/lib/firebase_auth_web.dart#L313

But that does not seem correct, it seems that we should be here? https://github.com/FirebaseExtended/flutterfire/blob/2eb0751269b679ced0c033823d896aa4e8daea82/packages/firebase_auth/firebase_auth_web/lib/firebase_auth_web.dart#L343

...so I'm not sure I understand the error but I still lean strongly towards "you are the first to use it and report back, and there is probably some trivial implementation error on my part" so I'd like to know more about why the line numbers don't meet expectations.

If it is not an implementation error I separately suspect this may have to do with the firebase-js-sdk versions not being compatible (because we did not mention anywhere what was needed!) -

It appears you need to set them yourself in your HTML:
https://github.com/FirebaseExtended/flutterfire/blob/no-nullsafety/packages/firebase_auth/firebase_auth_web/README.md#updating-indexhtml

Further it appears that the firebase 8.2.4 version may be the first one that works for useEmulator - can you either confirm you are already using this version (or higher), or if not then upgrade to it and test with it?
https://firebase.google.com/support/release-notes/js#version_824_-_january_21_2021

Note that for android, localhost will be remapped for you to 10.0.2.2 as a developer experience enhancement, so you may remove some of your code I think:

https://github.com/FirebaseExtended/flutterfire/blob/2eb0751269b679ced0c033823d896aa4e8daea82/packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart#L96-L105

@pxsanghyo
Copy link

@markhardy awesome, thanks for looking into it.

The firebase-js-sdk version is/was set at 8.2.7

  <script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-app.js"></script>
	<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-analytics.js"></script>
	<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-auth.js"></script>
	<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-firestore.js"></script>

  <script>
    var firebaseConfig = {};
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
  </script>

Thanks for the pointer about localhost being remapped to 10.0.2.2. Let me know if you need me to try anything else.

@mikehardy
Copy link
Contributor Author

@pxsanghyo you definitely want me versus Mark Hardy, though I am certain he is a nice person :-)
I'm a bit sad to find you are already using the correct version, as that would have been a great/quick win if simply updating that did it.
This looks like a real issue then - can you log it as a formal new issue here and tag me? That will help keep things organized here
Thanks!

@pxsanghyo
Copy link

@mikehardy lol sorry about that.

I created issue #5096. Thanks

@firebase firebase locked and limited conversation to collaborators Feb 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes plugin: auth type: enhancement New feature or request type: missing-feature A feature that is supported on the underlying Firebase SDK but has not been exposed to Dart API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.