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

Allow setting mock user to null #112

Merged
merged 6 commits into from
Sep 5, 2024
Merged

Allow setting mock user to null #112

merged 6 commits into from
Sep 5, 2024

Conversation

michaelowolf
Copy link
Contributor

@michaelowolf michaelowolf commented Aug 30, 2024

Resolves #110.

I've also tidied up some repeated logic when adding new user credentials to the various streams.

@michaelowolf
Copy link
Contributor Author

I'll fix conflicts today

@michaelowolf
Copy link
Contributor Author

@atn832 should be ready for review now

final decodedToken =
JwtDecoder.decode((await auth.currentUser!.getIdToken())!);
expect(decodedToken['role'], 'admin');
expect(decodedToken['bodyHeight'], 169);
await auth.signOut();

auth.mockUser = user;
Copy link
Owner

Choose a reason for hiding this comment

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

I think I have misnamed mockUser. It seems like mockUser is the user for the next signin, but, if signed in, also lets you set the current user (#80).

In the future, renaming mockUser to userOnNextSignIn, and exposing the currently signed in user to allow #80 's use case might be more appropriate.

I need to think about it more before changing the API and breaking people's projects.

Copy link
Owner

@atn832 atn832 Sep 2, 2024

Choose a reason for hiding this comment

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

If I think of mockUser as the userOnNextSignIn, it feels strange that signing out should set it to null. It will also break people's projects if they sign out and sign in again, like in this test.

Since you've allowed setting mockUser to null, would that be enough? Then you can implement your use case as :

auth.signIn(...);
auth.signOut();

auth.mockUser = null;
auth.signInAnonymously();

It's not perfect but it won't break any existing project until we figure out a better solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem - I definitely think my understanding of how mockUser is intended to be used was lacking. I am happy to revert some changes here as you suggest.

I need to specifically look at how we handle the case in #110 as per your suggestion though, as I'm not sure we have that fine grained control. I will check back shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@atn832 Having revisited our problem with your explanation of mockUser, I think your suggestion makes sense and works for us. I've reverted the change to clear the mock user now, and left in everything else. Let me know what you think.

return Future.value(userCredential);
}

void _notifyCredential(MockUserCredential? credential) {
Copy link
Owner

Choose a reason for hiding this comment

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

Nice!

@michaelowolf michaelowolf changed the title Clear mock user on sign out Allow setting mock user to null Sep 4, 2024
expect(auth.currentUser, isNull);
expect(auth.authStateChanges(), emitsInOrder([user, null]));
expect(auth.userChanges(), emitsInOrder([user, null]));
auth.mockUser = tUser;
Copy link
Owner

Choose a reason for hiding this comment

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

Since signing out does not set mockUser to null anymore, do you still need to set mockUser again if signing in with the same user? Maybe you can remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, you're right - this should have been removed too. I'll clean that up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, looks like you fixed it - I missed your other comment. Thanks!

Copy link
Owner

@atn832 atn832 left a comment

Choose a reason for hiding this comment

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

Looks good, there might be one line you can remove in the test. I'll merge and remove it myself. Thanks for your contribution!

@atn832 atn832 merged commit cf953b0 into atn832:master Sep 5, 2024
3 checks passed
@atn832
Copy link
Owner

atn832 commented Sep 5, 2024

I removed the line (b28ab70), then published both #111 and #112 to https://pub.dev/packages/firebase_auth_mocks/changelog#0141. Thanks again @michaelowolf !

@michaelowolf michaelowolf deleted the sign-out-clear-mock-user branch September 5, 2024 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

signOut doesn't clear mock user
2 participants