-
Notifications
You must be signed in to change notification settings - Fork 938
FR: please add function Auth.getCurrentUser() as a promise that will return current user or null #462
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
Comments
Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information. |
You can easily implement that on your own with a couple of lines:
|
@bojeil-google If the user does not signed in will that promise would ever fired for null? And I think this function is really common enough to included in the SDK |
Yes that promise will fire. |
I see but anyway if it really just a couple line and will be the same for everyone so why it not be inside SDK. Did you not consider it useful? And also that function would fire only one time at first call and will not fire again in second call am I right? What I want from At least it should have a function like |
I have no idea what you mean by: " consistently return null if it already checking in persistence storage, or return the actual current user after that". |
@bojeil-google If I don't misunderstand, the This is not what I want from What I want is
I want this But if I don't misunderstand, the function you present will never resolve if that function was called after firebase auth preparation is finished unless the user try to signed in |
If I misunderstand above, which means |
Please read the documentation, you presume some incorrect assumptions. If you want some custom function or listener that triggers based on your own custom requirements, we provide all the APIs you need to build that. If you are experiencing issues with the behavior of |
@bojeil-google I know that if I call I know that it would be surely be triggered whenever user would sign in by any means. But if user never sign in at all would it trigger that it change from Why is that? Is it changed from And if I register In the document https://firebase.google.com/docs/reference/js/firebase.auth.Auth I have seen the explanation only these
At this line
is very pinpoint that if user not doing any signin/signout at all it should not be triggered just from registering the observer Also in here is not mention about that too https://firebase.google.com/docs/auth/web/manage-users What document you think I should read? |
@bojeil-google Do you have a link to the document you mention about this behavior? |
This is the reference for This is the guide on how to use it: Any time you call this listener after initial state has been determined, it will trigger with the current state (null if no user signed in, and with a user if a user is signed in). If you don't believe, then just try it out yourself. |
@bojeil-google It not that I would not belief but it the API name itself is misleading so I got confused. I test it and it seem the real behavior is like you stated. But because it not mentioned in the document I don't know I should rely on it or not That's why I said if I misunderstand then it should be documented about this behavior. Because the API name is "onAuthStateChanged" but it will also firing one time when the auth state not really changed |
Fair enough. We should update the documentation and references to mention that. Thanks for pointing that out. |
I will 👍 this request, I just wasted a lot of time trying to find a method on the api to load the currently logged in user on page load before I found this issue. It needs to be mentioned that this will also load the user from the persistence if it is persisted. |
Here is modified version of @bojeil-google function that can be used anytime to get a promise that resolves into current user or null.
|
I've 👍 this request, too. As the Thaina, I also think the documentation about |
@atishpatel I've tried that but it deosnt work for me :/ |
Does anyone know if |
|
No idea... I'm using nuxt and have this as a pluggin:
|
sometimes causes inf recursion 🙃 |
bump. I'm finding many hours wasted on the auth state. I vote for a simple API the consistently gives us the user or null when we need it. |
See, authStateChanged has a purpose, and therefore it can be used to workaround the fact that .auth().currentUser is not a promise and it's value is not trustable (because it depends on asynchronous tasks), it's purpose is to watch changes at the auth state. getCurrentUser, method that doesn't exists currently, has a different purpose: to simply get the current user despite how many changes happened at the application until now. Dont take this wrong, please, but having methods implicates in having closed scopes of use, it's a basic pattern rule. Either making it possible to use async .auth.currentUser or .auth().getCurrentUser().then would be desired implementation. |
@zerobytes I just wrap it in a promise before calling it from other services. function getUser() {
return new Promise((resolve, reject) => {
let user = this.firebase.auth().currentUser;
return user ? resolve(user) : reject(new Error('No user signed in'));
} |
Hey @holmberd, @atishpatel Answer is more like what we need, and will do the job, but it's still a workaround. |
I haven't seen any such failures occur and therefore can't confirm. But it does look like there is a fair share of ambiguity in regards to the API usage in general. As I see it For me |
In angular this solution works for me:
I wait for firebase to complete authentication using authState. |
@avolkovi can I attempt to add async to sdk if you busy with other stuff ? |
@harshshredding yes, you're more than welcome, thanks! Please assign myself and @bojeil-google to review your PR once ready. |
Simply sharing -- in the hope that someone will find it useful :
|
Also for me, the two nulls behaviour of Two points:
Other than that, I think new JavaScript APIs should be Promise-based.
To me, the main point in this issue is @gamliela ’s comment from Apr 27th about the documentation. |
@akauppi I am not sure how point 1 relates to the earlier suggestion :
The return value makes sense even when the user logs out. |
Because that was not the use case needed around
I don't think anyone should ever rely on that because you really cannot be ensure that the No, you should always rely on promise based. Because that field actually unreliable |
Hello, everybody! How to check if a user can be authorized or not? If the user is not logged in, then render the application immediately, otherwise render the loading window and wait until the onAuthStateChanged event occurs. How to implement this behaviour? Thanks! |
In general, issues are for bug reporting and feature requests, and we recommend support or Stack Overflow or other forums for help with usage but: One common pattern is to set some local variable named If it is:
|
const getCurrentUser = firebase.auth.getRedirectResult().then(() => firebase.auth.currentUser); should achieve what you are looking for @Thaina. |
I don't think |
I am implementing a new promise-returning method called
|
This same issue (null meaning pending or signed out) exists in the flutter package as well. Is anyone aware of an issue having been logged that addresses this behaviour across all platforms? At the moment a delay seems to be the only functional work around and this seems really hacky! |
Yes, please. +1 for the user as a promise. This 2 year discussion should be enough! :D |
+1 this feature is really important. For anyone looking for a simple and effective solution for now, here you go
Of course be aware that the promise will only call once. You should unsubscribe the onAuthStateChanged call. |
+999.999.999 hehehe |
+100 this feature will save a lot of time. |
+1, why isn't it like this already |
There seem to have Are there anyone not satisfied with this approach? If there are not I would close this issue |
This tread shows what happens when the kids don't use Observables. |
@spock123 We don't want to always include 3rd party in every project especially when we need to mockup something fast I use Observables every time it possible. But it was not native to anything include firebase itself. I too wish that firebase would adopt reactive pattern into its API. But in the meantime we need to wrap everything in RxJS ourselves is not convenient approach And instead of blaming people not using observable, the one should be blamed is the api that not even using |
Hmm, I'm not sure if @spock123 meant the RxJS Observables. A general observer pattern would help here, of course promisifying would also help here. The blame here is on the maintainers of firebase: the problem is obvious, the solution should be simple enough I think. Our frustration comes from this issue not being addressed for almost 6 years now. This thread really shows what happens when people don't use observable patterns. |
Sorry @finnhvman @Thaina it was just some light fun. Cheers |
Doesn't appear to be present in the docs yet 😞 |
Auth.currentUser
object is unreliable to get user from persistence storage. If the authentication system could be pending and don't really have ensure finishing endpoint then you should have a promise for itSo I want to have promise function
Auth.getCurrentUser()
in addition toonAuthStateChange
that would eventually return user but would return null if no user exist in persistence storage or user require sign in from expired tokenThe text was updated successfully, but these errors were encountered: