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

[ServerApp] Update feature branch Auth implementation to use the authIdToken #7944

Merged
merged 47 commits into from
Jan 26, 2024

Conversation

DellaBitta
Copy link
Contributor

@DellaBitta DellaBitta commented Jan 12, 2024

Discussion

Add support for logging-in users with the FirebaseServerApp's authIdToken.

Testing

Local project testing client-side created users, passing idTokens to serverApps, and logging in the user. Tested with multiple users and multiple instances of FirebaseServerApps w/ Auth.

CI tests (added integration tests).

API Changes

N/A

DellaBitta and others added 30 commits September 19, 2023 11:31
revert name: undefined.
Swap undefineds.
Remove the new FirebaseServerApp from the app-types. App-types are meant only for compat, and FirebaseServerApp will not be part of compat.
DellaBitta and others added 4 commits January 16, 2024 11:27
Add integration tests for the automatic login of Auth users when initializing Auth with an instance of FirebaseServerApp.
@DellaBitta DellaBitta marked this pull request as ready for review January 22, 2024 18:47
@@ -119,6 +119,11 @@ export function initializeServerApp(options: FirebaseOptions | FirebaseApp, conf
// @internal (undocumented)
export function _isFirebaseApp(obj: FirebaseApp | FirebaseOptions): obj is FirebaseApp;

// Warning: (ae-forgotten-export) The symbol "FirebaseServerAppImpl" needs to be exported by the entry point index.d.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're exporting this out of @firebase/app you should also export FirebaseServerAppImpl, as its typing depends on that type, otherwise you could run into some TS errors. The other alternative is to be a little looser with the typings and not do the obj is FirebaseServerAppImpl and just do boolean.

Copy link
Contributor Author

@DellaBitta DellaBitta Jan 24, 2024

Choose a reason for hiding this comment

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

Ah yes, I was exporting FirebaseServerAppImpl so that Auth could get the authIdToken from it. I didn't realize that it would then be part of the public API, too, but that makes total sense that it is.

I'll work to make it FirebaseServerAppImpl to say withinapp only, and figure out something else for auth.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, to be clear, FirebaseServerAppImpl isn't being exported at the moment, as far as I can tell, which is what api-extractor is warning you about. _isFirebaseServerAppImpl is being exported. But since the typing of _isFirebaseServerAppImpl depends on FirebaseServerAppImpl, api-extractor is complaining that you have to export that too. The way to get around it is to not use the obj is return type and return a boolean instead.

The internal.ts file isn't "public" in that it's not meant to be used be users of the SDK, it's meant to export "internal" methods used by other packages, like auth, so I think this is fine to export this function in that file (if you check dist/ after building you should see it show up in the "internal" bundle and not in the main bundle). The only problem is that there may possibly be some TS issue at some point if you don't resolve this warning, so I think the simplest way is to change the return type to boolean.

Copy link
Contributor Author

@DellaBitta DellaBitta Jan 25, 2024

Choose a reason for hiding this comment

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

Right, I was saying to fix the error I would have needed to export FirebaseServerAppImpl, and I didn't want to do that.

This should be fixed now. I've updated the function to simple by isFirebaseServerApp and not isFirebaseServerAppImpl. FirebasServerApp is already a public interface so we should be good to go here.

packages/auth/src/core/auth/initialize.ts Outdated Show resolved Hide resolved
response,
idToken
);
await authInternal._updateCurrentUser(user);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could there be a race condition here? _initializeAuth also calls another hanging promise, provider.initialize which I think calls _initializeAuthInstance which eventually tries to check for a user in persistent storage and will try to _updateCurrentUser if it finds anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FirebaseServerApps cannot be created in Browser environments. Is there persistence outside of browser environments?

I did some testing and printed out the persistence array in _initializeAuthInstance and it appears to be empty.

Copy link
Contributor

Choose a reason for hiding this comment

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

Whether it finds a user or not, does it eventually call _updateCurrentUser() anyway, with null if the user isn't found? Not sure if it does, but if it does, seems like that could overwrite a value you set here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't seem to, still I think it should have a more definitive implementation. I'll work on this topic on Friday.

Copy link
Contributor Author

@DellaBitta DellaBitta Jan 26, 2024

Choose a reason for hiding this comment

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

Ok, I now suppress loading users from persistence upon Auth init for FirebaseServerApps. Looks like it works properly with multiple apps not interfering with each other, etc.

packages/auth/src/core/user/user_impl.ts Show resolved Hide resolved
In the tests, attach emulators to Auth instances created via FirebaseServerApps.

In Auth initialization, wait a tick before kicking off the automatic loading of users by idToken. This should give the app enough time to attach an emulator.
@DellaBitta DellaBitta requested a review from hsubox76 January 26, 2024 16:49
Copy link
Contributor

@hsubox76 hsubox76 left a comment

Choose a reason for hiding this comment

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

LG, nice work!

@DellaBitta DellaBitta merged commit fc44b08 into feature-firebaseserverapp Jan 26, 2024
41 checks passed
@DellaBitta DellaBitta deleted the ddb-serverapp-use-auth-token branch January 26, 2024 19:22
@firebase firebase locked and limited conversation to collaborators Feb 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants