Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native with Firebase Authentication

I'm aware that using Twitter/Facebook Firebase authentication with React Native throws an error since obviously that's not the right env to open up a new auth window. Also, Email/Password auth should also fail since it's reliant upon LocalStorage which React Native doesn't support. Is the best way to implement any sort of Firebase authentication in React Native to create a Swift/ObjC bridge then really just use the Swift/ObjC Firebase auth library? If so, does anyone have any examples of doing this?

like image 576
Tyler McGinnis Avatar asked Jul 09 '15 14:07

Tyler McGinnis


1 Answers

Update: As of Firebase 3.x, Firebase Authentication will correctly persist users in React Native apps.

Ahh, I didn't realize that React Native didn't polyfill localstorage. So if I understand your question right, email/password auth with Firebase works fine in React Native, except it is not persisted across app starts, so the user has to log in every time.

One possible solution would be to manually persist the auth token yourself. Something like:

  1. Call authWithPassword(...) as usual.
  2. In the onComplete handler, retrieve authData.authToken and store it locally (e.g. using AsyncStorage).
  3. On next app start, retrieve the auth token from storage and call authWithCustomToken(authToken, ...).

We'll look into making Firebase do this automatically, but it's a bit tricky to do this without affecting other consumers of Firebase besides react. I've added this to our internal bug database though and we'll see what we can come up with. We'll update this answer when we have a solution. Thanks!

like image 182
Michael Lehenbauer Avatar answered Oct 26 '22 20:10

Michael Lehenbauer