A firestack v3 react-native implementation
A firestack v3 react-native implementation
Firestack makes using the latest Firebase straight-forward.
Firestack is a light-weight layer sitting atop the native Firebase libraries for iOS and Android and mirrors the React Native JS api as closely as possible.
For a detailed discussion of how Firestack works as well as how to contribute, check out our contribution guide.
We have a working application example available in at fullstackreact/FirestackApp. Check it out for more details about how to use Firestack.
Firebase is awesome and it's combination with the Google Cloud Platform makes it super awesome. Sadly, the latest version of Firebase requires the window object. That's where Firestack comes in! Firestack provides a really thin layer that sits on top of the native Firebase SDKs and attempts to use the JavaScript library as much as possible rather than reinventing the wheel.
Getting react-native-firestack up and running in your app should be a 2 step process + 1 for each platform.
npm packagereact-native link react-native-firestackMainApplication.java for the FirestackPackage() line.Those steps in more detail:
Install the npm package with:
npm install react-native-firestack --saveTo use Firestack, we'll need to have a development environment that includes the same prerequisites of Firebase.
Unfortunately, due to AppStore restrictions, we currently do not package Firebase libraries in with Firestack. However, the good news is we've automated the process (with many thanks to the Auth0 team for inspiration) of setting up with cocoapods. This will happen automatically upon linking the package with react-native-cli.
Remember to use the ios/[YOUR APP NAME].xcworkspace instead of the ios/[YOUR APP NAME].xcproj file from now on.
We need to link the package with our development packaging. We have two options to handle linking:
React native ships with a link command that can be used to link the projects together, which can help automate the process of linking our package environments.
react-native link react-native-firestackUpdate the newly installed pods once the linking is done:
cd ios && pod update --verboseIf you prefer not to use rnpm, we can manually link the package together with the following steps, after npm install:
In XCode, right click on Libraries and find the Add Files to [project name].
Add the node_modules/react-native-firestack/ios/Firestack.xcodeproj
Ensure that the Build Settings of the Firestack.xcodeproj project is ticked to All and it's Header Search Paths include both of the following paths and are set to recursive:
$(SRCROOT)/../../react-native/React
$(SRCROOT)/../node_modules/react-native/React
${PROJECT_DIR}/../../../ios/Pods
Setting up cocoapods
Since we're dependent upon cocoapods (or at least the Firebase libraries being available at the root project -- i.e. your application), we have to make them available for Firestack to find them.
Using cocoapods is the easiest way to get started with this linking. Add or update a Podfile at ios/Podfile in your app with the following:
source 'https://github.com/CocoaPods/Specs.git'
[
'Firebase/Core',
'Firebase/Auth',
'Firebase/Storage',
'Firebase/Database',
'Firebase/RemoteConfig',
'Firebase/Messaging'
].each do |lib|
pod lib
end
target '[project name]' do
end
# target '[project test name]' do
# endThen you can run (cd ios && pod install) to get the pods opened. If you do use this route, remember to use the .xcworkspace file.
If you don't want to use cocoapods, you don't need to use it! Just make sure you link the Firebase libraries in your project manually. For more information, check out the relevant Firebase docs at https://firebase.google.com/docs/ios/setup#frameworks.
Full Android support is coming soon, as it currently supports a smaller feature-set than the iOS version. Just as we do with iOS, we'll need to install the library using npm and call link on the library:
react-native link react-native-firestackFirestack includes the Firebase libraries and will link those directly into our project automatically.
To install react-native-firestack manually in our project, we'll need to import the package from io.fullstack.firestack in our project's android/app/src/main/java/com/[app name]/MainApplication.java and list it as a package for ReactNative in the getPackages() function:
package com.appName;
// ...
import io.fullstack.firestack.FirestackPackage;
// ...
public class MainApplication extends Application implements ReactApplication {
// ...
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new FirestackPackage()
);
}
};
// ...
}We'll also need to list it in our android/app/build.gradle file as a dependency that we want React Native to compile. In the dependencies listing, add the compile line:
dependencies {
compile project(':react-native-firestack')
}Add to AndroidManifest.xml file
+
+
+
+
+
+
+
+
+
+ The Firestack library is intended on making it easy to work with Firebase and provides a small native shim to the Firebase native code.
To add Firebase to your project, make sure to create a project in the Firebase console
Each platform uses a different setup method after creating the project.
After creating a Firebase project, click on the Add Firebase to your iOS app and follow the steps from there to add the configuration file. You do not need to set up a cocoapods project (this is already done through firestack). Make sure not to forget the Copy Files phase in iOS.
Download the Firebase config file and place it in your app directory next to your app source code:
Once you download the configuration file, make sure you place it in the root of your Xcode project. Every different Bundle ID (aka, even different project variants needs their own configuration file).
Lastly, due to some dependencies requirements, Firestack supports iOS versions 8.0 and up. Make sure to update the minimum version of your iOS app to 8.0.
There are several ways to setup Firebase on Android. The easiest way is to pass the configuration settings in JavaScript. In that way, there is no setup for the native platform.
If you prefer to include the default settings in the source of your app, download the google-services.json file provided by Firebase in the Add Firebase to Android platform menu in your Firebase configuration console.
Next you'll have to add the google-services gradle plugin in order to parse it.
Add the google-services gradle plugin as a dependency in the project level build.gradle
android/build.gradle
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
}In your app build.gradle file, add the gradle plugin at the VERY BOTTOM of the file (below all dependencies)
android/app/build.gradle
apply plugin: 'com.google.gms.google-services'After creating a Firebase project and installing the library, we can use it in our project by importing the library in our JavaScript:
import Firestack from 'react-native-firestack'We need to tell the Firebase library we want to configure the project. Firestack provides a way to configure both the native and the JavaScript side of the project at the same time with a single command:
const firestack = new Firestack();We can pass custom options by passing an object with configuration options. The configuration object will be generated first by the native configuration object, if set and then will be overridden if passed in JS. That is, all of the following key/value pairs are optional if the native configuration is set.
| option | type | Default Value | Description |
|---|---|---|---|
| debug | bool | false | When set to true, Firestack will log messages to the console and fire debug events we can listen to in js |
| bundleID | string | Default from app [NSBundle mainBundle] |
The bundle ID for the app to be bundled with |
| googleAppID | string | "" | The Google App ID that is used to uniquely identify an instance of an app. |
| databaseURL | string | "" | The database root (i.e. https://my-app.firebaseio.com) |
| deepLinkURLScheme | string | "" | URL scheme to set up durable deep link service |
| storageBucket | string | "" | The Google Cloud storage bucket name |
| androidClientID | string | "" | The Android client ID used in Google AppInvite when an iOS app has it's android version |
| GCMSenderID | string | "" | The Project number from the Google Developer's console used to configure Google Cloud Messaging |
| trackingID | string | "" | The tracking ID for Google Analytics |
| clientID | string | "" | The OAuth2 client ID for iOS application used to authenticate Google Users for signing in with Google |
| APIKey | string | "" | The secret iOS API key used for authenticating requests from our app |
For instance:
const configurationOptions = {
debug: true
};
const firestack = new Firestack(configurationOptions);
firestack.on('debug', msg => console.log('Received debug message', msg))Firestack is broken up into multiple parts, based upon the different API features that Firebase provides.
All methods return a promise.
Firestack handles authentication for us out of the box, both with email/password-based authentication and through oauth providers (with a separate library to handle oauth providers).
Android requires the Google Play services to installed for authentication to function.
Firebase gives us a reactive method for listening for authentication. That is we can set up
No open issues yet, or sync has not completed.