Skip to content

Support getAuth() after getDatabase() #4889

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

Merged
merged 2 commits into from
May 12, 2021
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented May 10, 2021

This changes the RTDB SDK to allow getAuth() to be called after getDatabase(). Unlike Firestore, it does not go back into blocking mode if Auth is initialized after startup. This would require us to rewrite the entire networking layer to be asynchronous and I suspect that this simple change is enough to cover most use cases.

The simple fix here works because the initial connection attempt in the networking layer is asynchronous. Only was this.connected is set to true do we flip to a synchronous behavior and send all requests immediately.

@schmidt-sebastian schmidt-sebastian requested a review from jsdt as a code owner May 10, 2021 21:49
@changeset-bot
Copy link

changeset-bot bot commented May 10, 2021

⚠️ No Changeset found

Latest commit: 7ccc471

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@google-oss-bot
Copy link
Contributor

Size Analysis Report

Affected Products

Diffs between base commit (cc7207e) and head commit (147565b) are too large (479,509 characters) to display.

Please check below links to see details from the original test log.

Copy link
Member

@Feiyang1 Feiyang1 left a comment

Choose a reason for hiding this comment

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

Thanks for making the PR. Database now works well with synchronous Auth initialization regardless if it happens before or after getDatabase(), but it still has problem with asyn Auth initialization. Considering the following example:

const database = getDatabase();
const docRef = ref(database, 'Foo');

setTimeout(() => {
    getAuth();

    // access denied
    get(docRef).then(d => {
        console.log('database get async', d.val());
    });

    onAuthStateChanged(getAuth(), (user) => {
        // access denied even in the onAuthStateChanged callback
        get(docRef).then(d => {
            console.log('database get async', d.val());
        });
    });
}, 1000)

The behavior is quite unexpected - both get() should send authenticated requests, but they did not. And I was really surprised that the get() in onAuthStateChanged() is also unauthenticated.

IIUC, AngularFire loads Auth asynchronously, so it's important to make Database work correctly with asyn auth initialization. @jamesdaniels, anything to add?

@@ -36,13 +36,25 @@ export class AppCheckTokenProvider {
) {
this.appCheck = appCheckProvider?.getImmediate({ optional: true });
if (!this.appCheck) {
appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck));
appCheckProvider?.onInit(appCheck => (this.appCheck = appCheck));
Copy link
Member

Choose a reason for hiding this comment

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

AppCheck does not use Provider.initialize() yet, so we need to use get() instead of onInit() for now. We do need to change AppCheck to use Provider.initialize() and change here accordingly, but let's do that in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted this part for now.

Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

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

Supporting synchronous loading of the RTDB SDK with asynchronous loading of Auth will be quite tricky and we should probably attempt to do this in a separate PR if @jamesdaniels deems it necessary. Lazy-loading Auth after getDatabase() should already work as long as the RTDB has not been used by the time getAuth() is called.

@@ -36,13 +36,25 @@ export class AppCheckTokenProvider {
) {
this.appCheck = appCheckProvider?.getImmediate({ optional: true });
if (!this.appCheck) {
appCheckProvider?.get().then(appCheck => (this.appCheck = appCheck));
appCheckProvider?.onInit(appCheck => (this.appCheck = appCheck));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted this part for now.

@google-oss-bot
Copy link
Contributor

Binary Size Report

Affected SDKs

  • @firebase/analytics

    Type Base (cc7207e) Head (d427644) Diff
    esm2017 ? 18.7 kB ? (?)
    main ? 24.2 kB ? (?)
    module ? 23.4 kB ? (?)
  • @firebase/api-documenter

    Type Base (cc7207e) Head (d427644) Diff
    main ? 3.72 kB ? (?)
  • @firebase/app

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 10.9 kB ? (?)
    esm2017 ? 9.63 kB ? (?)
    lite ? 8.95 kB ? (?)
    lite-esm2017 ? 7.93 kB ? (?)
    main ? 9.99 kB ? (?)
    module ? 10.9 kB ? (?)
    react-native ? 9.70 kB ? (?)
  • @firebase/app-check

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 20.7 kB ? (?)
    esm2017 ? 17.1 kB ? (?)
    main ? 20.7 kB ? (?)
    module ? 20.2 kB ? (?)
  • @firebase/auth

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 181 kB ? (?)
    main ? 181 kB ? (?)
    module ? 181 kB ? (?)
  • @firebase/component

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 7.84 kB ? (?)
    esm2017 ? 5.88 kB ? (?)
    main ? 8.18 kB ? (?)
    module ? 7.84 kB ? (?)
  • @firebase/database

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 296 kB ? (?)
    esm2017 ? 265 kB ? (?)
    main ? 298 kB ? (?)
    module ? 296 kB ? (?)
  • @firebase/database-compat

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 86.3 kB ? (?)
    main ? 102 kB ? (?)
    module ? 86.3 kB ? (?)
  • @firebase/database-exp

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 246 kB ? (?)
    main ? 278 kB ? (?)
    module ? 246 kB ? (?)
  • @firebase/firestore

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 284 kB ? (?)
    esm2017 ? 226 kB ? (?)
    main ? 531 kB ? (?)
    module ? 284 kB ? (?)
    react-native ? 226 kB ? (?)
  • @firebase/firestore-compat

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 28.6 kB ? (?)
    main ? 37.6 kB ? (?)
    module ? 28.6 kB ? (?)
    react-native ? 28.3 kB ? (?)
  • @firebase/firestore-exp

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 224 kB ? (?)
    main ? 507 kB ? (?)
    module ? 224 kB ? (?)
    react-native ? 224 kB ? (?)
  • @firebase/firestore-lite

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 72.4 kB ? (?)
    main ? 147 kB ? (?)
    module ? 72.4 kB ? (?)
    react-native ? 72.6 kB ? (?)
  • @firebase/firestore/bundle

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 290 kB ? (?)
    esm2017 ? 176 kB ? (?)
    main ? 527 kB ? (?)
    module ? 290 kB ? (?)
    react-native ? 176 kB ? (?)
  • @firebase/firestore/memory

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 216 kB ? (?)
    esm2017 ? 172 kB ? (?)
    main ? 325 kB ? (?)
    module ? 216 kB ? (?)
    react-native ? 172 kB ? (?)
  • @firebase/firestore/memory-bundle

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 224 kB ? (?)
    esm2017 ? 176 kB ? (?)
    main ? 322 kB ? (?)
    module ? 224 kB ? (?)
    react-native ? 176 kB ? (?)
  • @firebase/functions

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 10.6 kB ? (?)
    esm2017 ? 8.15 kB ? (?)
    main ? 11.0 kB ? (?)
    module ? 10.6 kB ? (?)
  • @firebase/installations

    Type Base (cc7207e) Head (d427644) Diff
    esm2017 ? 16.6 kB ? (?)
    main ? 22.2 kB ? (?)
    module ? 21.6 kB ? (?)
  • @firebase/logger

    Type Base (cc7207e) Head (d427644) Diff
    esm2017 ? 3.25 kB ? (?)
    main ? 5.38 kB ? (?)
    module ? 4.65 kB ? (?)
  • @firebase/messaging

    Type Base (cc7207e) Head (d427644) Diff
    esm2017 ? 26.2 kB ? (?)
    main ? 34.9 kB ? (?)
    module ? 34.4 kB ? (?)
  • @firebase/performance

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 27.7 kB ? (?)
    esm2017 ? 25.9 kB ? (?)
    main ? 28.0 kB ? (?)
    module ? 27.7 kB ? (?)
  • @firebase/polyfill

    Type Base (cc7207e) Head (d427644) Diff
    main ? 747 B ? (?)
    module ? 705 B ? (?)
  • @firebase/remote-config

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 22.4 kB ? (?)
    esm2017 ? 17.4 kB ? (?)
    main ? 23.0 kB ? (?)
    module ? 22.4 kB ? (?)
  • @firebase/rules-unit-testing

    Type Base (cc7207e) Head (d427644) Diff
    main ? 14.8 kB ? (?)
  • @firebase/storage

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 63.4 kB ? (?)
    esm2017 ? 54.6 kB ? (?)
    main ? 63.8 kB ? (?)
    module ? 63.4 kB ? (?)
  • @firebase/storage-compat

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 10.2 kB ? (?)
    main ? 29.1 kB ? (?)
    module ? 10.2 kB ? (?)
  • @firebase/storage-exp

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 51.5 kB ? (?)
    main ? 52.6 kB ? (?)
    module ? 51.5 kB ? (?)
  • @firebase/util

    Type Base (cc7207e) Head (d427644) Diff
    browser ? 21.2 kB ? (?)
    esm2017 ? 20.0 kB ? (?)
    main ? 25.8 kB ? (?)
    module ? 21.2 kB ? (?)
  • @firebase/webchannel-wrapper

    Type Base (cc7207e) Head (d427644) Diff
    esm2017 ? 39.5 kB ? (?)
    main ? 46.9 kB ? (?)
    module ? 40.8 kB ? (?)
  • firebase

    Click to show 16 binary size changes.
    Type Base (cc7207e) Head (d427644) Diff
    firebase-analytics.js ? 35.8 kB ? (?)
    firebase-app-check.js ? 22.0 kB ? (?)
    firebase-app.js ? 21.7 kB ? (?)
    firebase-auth.js ? 177 kB ? (?)
    firebase-database.js ? 187 kB ? (?)
    firebase-firestore.js ? 332 kB ? (?)
    firebase-firestore.memory.js ? 267 kB ? (?)
    firebase-functions.js ? 10.7 kB ? (?)
    firebase-installations.js ? 19.3 kB ? (?)
    firebase-messaging.js ? 41.0 kB ? (?)
    firebase-performance-standalone.es2017.js ? 73.1 kB ? (?)
    firebase-performance-standalone.js ? 49.7 kB ? (?)
    firebase-performance.js ? 38.3 kB ? (?)
    firebase-remote-config.js ? 36.9 kB ? (?)
    firebase-storage.js ? 41.6 kB ? (?)
    firebase.js ? 888 kB ? (?)

Test Logs

@schmidt-sebastian schmidt-sebastian merged commit 8bece48 into master May 12, 2021
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/lazyauth branch May 12, 2021 01:05
@firebase firebase locked and limited conversation to collaborators Jun 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants