Firebase + React Native: Offline authentication

爱⌒轻易说出口 提交于 2019-12-04 03:15:01

Whilst the Firebase JS SDK is great and does generally work in React Native, it is mainly built for web platforms and therefore is not the most comprehensive solution for usage inside a React Native environment e.g. there are quite a few Firebase services you'll be unable to use with the Web SDK. See the comparison table here.

You can, however, run with the native Android/iOS Firebase SDKs and have a bridge between them and your JavaScript code (i.e. a react native module).

Thankfully you don't have to implement this yourself, as there are already modules out there to do this for you:

react-native-firebase for example mirrors the web SDKs API on the JavaScript side but executes on the native side using the native Android & iOS Firebase SDKs. It's compatible with any existing Firebase Web SDK logic that you may have already implemented and is intended as a drop-in replacement for the web SDK.

This library supports your use case with auth persistence and offline capabilities:

import firebase from 'react-native-firebase';

const instance = firebase.initializeApp({
  persistence: true
});

// can also use `keepSynced` / `setPersistence` methods:
// instance.database().ref('/someref').keepSynced();
// instance.database().setPersistence(true);

export default instance;

(disclaimer: I am the author of react-native-firebase)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!