sidferreira / aor-firebase-client

Firebase client for Admin on Rest
MIT License
57 stars 29 forks source link

User doesn't persist #26

Closed thomasorten closed 6 years ago

thomasorten commented 7 years ago

I can't get the user to persist.

I can login fine, but upon browser refresh, firebase.auth().currentUser is null. The token firebaseToken is set in localStorage.

Everything else is pretty similar to your demo.

And, how is data structured in firebase? E.g. from your demo?

sidferreira commented 7 years ago

Noticed it too, but I'm wondering if this is a change in Firease's API.

I'm preparing a new version, with a lot of breaking changes, which will allow any schema. Will add more examples too.

ejferg commented 6 years ago

@sidferreira @thomasorten This is how I solved the problem.

https://gist.github.com/ejferg/c025e202868da4761e737446166579c6

sidferreira commented 6 years ago

@ejferg Thanks a lot man! I think today I'll be able to check this and other stuff and update the tool.

afilp commented 6 years ago

@sidferreira I noticed that this worked for the 1st refresh but not for the 2nd, since auth is null.

sidferreira commented 6 years ago

will take a look!

afilp commented 6 years ago

@sidferreira I think this may have been my mistake, just mentioning it so that you don't lose time (I had some custom code there).

afilp commented 6 years ago

@sidferreira I am afraid I still have the problem, auth is null. I notice that this happens in every hot reloading when I develop the app and when I am in a route that needs to get data and therefore uses the if (type === AUTH_CHECK) { code.

It is interesting that the logout does not happen if I just move to the "Dashboard" route before I make a change in the code and then go to the page that fetches data. In this case, the auth is retained.

afilp commented 6 years ago

I think I found something. It has to do with the timeout in this line:

https://github.com/sidferreira/aor-firebase-client/blob/d58d737f718944f3e3c30c7ef40813de7ec0d363/src/authClient.js#L42

const timeout = (!firebase.auth().currentUser && localStorage.getItem(config.localStorageTokenName)) ? 1000 : 100

It appears that if I increase the 1000 value to 4000 then I am not forced to logout.

Which means that hot reloading needs a little more time than the normal production run?

I did the following change for DEV only and it works (in a hacky way):

const timeout = (!firebase.auth().currentUser && localStorage.getItem(config.localStorageTokenName)) ? (process.env.NODE_ENV !== 'production' ? 4000 : 1000) : 100

I also made a PR here: https://github.com/sidferreira/aor-firebase-client/pull/30

leomyth330 commented 6 years ago

This happens in v0.3.1. Login works fine and close the tab, open new tab requires me to login again.

leomyth330 commented 6 years ago

Well, I had to change firebase auth persistence storage to local from session. https://firebase.google.com/docs/auth/web/auth-state-persistence