DEV Community

Discussion on: Authenticate users with firebase and react.

Collapse
 
arnelamo profile image
Arne Pedersen

Hey, Tallan. Thanks for a nice tutorial.
When I came to the part where you store the token in localStorage, I couldn't find the token at the same place in the res Object. I then started Googling this and it seems firebase automatically stores the user's credentials in local storage, and reloads it from there when the app restarts/page reloads, under Storage -> IndexedDB.

If the response object has changed since you created this tutorial, what would you suggest I do to move forward?

Collapse
 
tallangroberg profile image
Tallan Groberg

I’m going to have to look at the res object from firebase and all of application storage in order to give you the best answer, but I did notice that the token was in the indexDB when I wrote this tutorial. I found it easier to dig through object from firebase and save it to local storage rather than try and access indexDB. My recommendation is the dig through the object and save the token to local storage, however it is worth assessing the difficulty of cutting out the middle man and just using indexDB.

I will give you a better answer than this when I have the time to look at the code again.

Collapse
 
arnelamo profile image
Arne Pedersen • Edited

Wow, that's fantastic. I'm looking forward to hear what you find.

I can see that some people recommend useEffect and something called onAuthStateChanged, like so:

useEffect(() => {
   const unsub = firebase.auth().onAuthStateChanged((user) => {
      user ? setUser(user) : setUser(null);
    });
    return () => {
      unsub();
    };
  });
  return user;
};

I guess this is using the stored data in the indexedDB(?), but if you would consider this as a viable option - how would you recommend integrating this with the context? If you could comment on this as well, it would be really awesome!