0

I have a react native app that users can login or signup through it, I use firebase to log them in but I don't understand what am I supposed to do with the returned object from firebase.auth().signInWithEmailAndPassword(email, pass); and createUserWithEmailAndPassword(email, pass);.

Am I supposed to use one of the parameters it returns? how?

Is each backend call (my backend not firebase's) supposed to be with one of the strings it returns?

Also which strings should I save on local storage so the users won't have to login again? I set firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
shinzou
  • 5,850
  • 10
  • 60
  • 124
  • Answer to you second question below. The first question is unclear to me. If you're having a problem, don't just describe what you do. Instead include the [minimum code that reproduces the problem](http://stackoverflow.com/help/mcve) in your question. That is the fastest way to get help. – Frank van Puffelen Aug 05 '17 at 19:25
  • It's unclear what am I supposed to do with a successful login, am I supposed to save the `firebaseUser.getIdToken()`? This also has a strange structure (and I can't stringify it since it has a circular structure), the key of the long string is "wa", should I add that "wa" string to http headers? @FrankvanPuffelen – shinzou Aug 05 '17 at 20:48
  • No you don't have to do anything. If you have a problem, share the minimal code that reproduces the problem. If you don't have a problem, great: carry on hacking. – Frank van Puffelen Aug 05 '17 at 21:59
  • The problem is that I need to use a successful auth, just having a successful auth and not using it is meaningless don't you think? It's like writing `int x = 3` and not using x. So I don't have a code or a minimal example because I don't know what to do with that `x`, or in our case the successful auth. @FrankvanPuffelen – shinzou Aug 06 '17 at 06:47

2 Answers2

2

Also which strings should I save on local storage so the users won't have to login again? I set firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);

This ensures that Firebase persists the authentication token in local storage. You don't need to do anything else. When the app restarts, Firebase automatically finds the token in local storage and re-authenticates the user with that information.

Most likely you will need to add a listener to onAuthStateChanged() to ensure you can update the UI of your app to this authentication state. For more on this see getting the current user in the Firebase documentation.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Will be great if you can answer this part: "Is each backend call (my backend not firebase's) supposed to be with one of the promises it returns? " Even I am not very clear with this – gonephishing Aug 05 '17 at 19:33
  • See my comment to your question. – Frank van Puffelen Aug 05 '17 at 19:38
  • I was looking for something similar to this: https://stackoverflow.com/questions/29240940/how-do-you-authenticate-a-server-to-firebase Documentation pages don't have it very clear – gonephishing Aug 05 '17 at 19:49
  • Does it save to local storage also in React Native? I imported firebase as you would for a web app. It should mention that in the docs.. – shinzou Aug 05 '17 at 20:22
1

In the case of a SPA (Single Page application), the returned object from firebase.auth().signInWithEmailAndPassword(email, pass); should be used to set the user name, email fields and display photographs inside your protected app pages. Also, since the user is in signed in state you can display private links inside this promise. You can also update user's profile inside this promise.

In the case of a multi page application, you might check the profile verification status and then redirect to your app's home page on the basis of the same.

You are supposed to get the ID token in your backend to identify valid requests

Firebase automatically stores the current user data in local storage which persists till the user logs out or the localStorage gets corrupted(?). You can confirm this from the fact that firebase auth does not work in case of Safari private browsing mode as it doesn't support localStorage methods. In short, nothing has to be done on your part to ensure data persists in localStorage, Firebase uses onAuthStaeChanged event listener to toggle sign in stage for a given user across all registered devices.

gonephishing
  • 1,388
  • 3
  • 18
  • 45
  • 1
    Please don't use the `then` from signInWithEmailAndPassword()` to update the UI. Doing so meant that the user will have to re-authenticate every time they load the page/app, even when the auth state was persisted. Use an `onAuthStateChanged()` callback to handle sign-in/sign-out state changes, and (possibly) a `catch()` to handle errors after the call to `signInWithEmailAndPassword()`. – Frank van Puffelen Aug 05 '17 at 19:41
  • Yes, my bad. It works for me because post sign in, I update the firebase userProfile, and then redirect to my app's home page which leads to a complete page reload. Though on reload I assumed it took cached localStorage data and not re-authentication. – gonephishing Aug 05 '17 at 19:55
  • Yeah, that last bit is crucial. It's not always wrong to handle `then()` (it's there for a good reason). But developers new to Firebase often use it for the wrong/naive reason(not their fault btw, clearly the API lures you into thinking that way), which leads to extra friction for their users. :-/ – Frank van Puffelen Aug 05 '17 at 20:12