Updated Oct 25: Switched from using “Callbacks” to “Promises” — ( Folder structure changed, api.js file deleted, actions.js and reducer.js updated)
In Part 1, we registered our app with Firebase and Facebook and also created our app and installed the required dependencies. In this tutorial, we will create the app’s directory, redux actions, reducers and api.
Now, that we have gotten that out of the way, LET THE CODING BEGIN!
Step 3: Create Folder Structure
In your project root create an app folder. For this project, I will be organizing the directory by feature/module instead of by type. To find out more about organising by feature/module, read this article or this or this or this to get a better understanding and see the advantages of organizing your project this way.
Setup the app folder with folders and files just like the image below.
Before we continue, we need to update the index file in our auth and home modules.
Update both index files with the code below.
Step 4: Setup
In this step, we will set up the back bone of our app
In the config folder, create the constants.js file and populate it with your Firebase app config information and your Facebook app id.
In the app/config folder, create a firebase.js file. In this file we will create a firebase singleton by creating an instance of the Firebase SDK and configure it with our firebase app config info so we can import it from anywhere in our app.
The reducers are the in charge of updating the state of our app. In the redux folder, create the rootReducer.js file. In this file, we will import the reducers from our modules and use redux combineReducers function to merge them together into a single state object that we will later use to create our redux store.
In the redux folder, create the store.js file. In this file we will create our redux store by importing our root reducer and injecting our store enhancers, i.e. Redux Thunk.
Step 5: Backend ( Actions and Reducer)
Before we create our actions, we will declare the action types to be used, in the auth module folder, update actionTypes.js file.
In the auth module folder, update actions.js file. Each function in our actions file is in charge of calling the appropriate Firebase API function and carrying out the right action based on the response returned.
The register function takes in the users data and a callback function as parameter, it extracts the email password and username from the data object and calls Firebase createUserWithEmailAndPassword function to create a new user. If successful, the username and the uid for the newly created user is passed to the createUser function in order to create the user object in the Realtime Database.
The login function is similar to the register function but it calls Firebase signInWithEmailAndPassword function to log the user in. If successful, the user object is returned. The user object is then passed to the getUser function in order to retrieve the user object form the Realtime Database.
In the auth module folder, update reducer.js file. Our reducer will be responding to 2 different action types, LOGIN_SUCCESS, LOGGED_OUT. It’s up to the reducer to decide if it needs to modify the app state or not based on the action type.
For this action type, the reducer sets the isLoggedIn state variable to true, sets the user state variable and stores the user object in the AsyncStorage.
The reducer sets the isLoggedIn state to false, sets the user state variable to null and removes the user object from the AsyncStorage.
In Part 3, we will create our scenes and inject the Redux store into the app.
Thats all folks!
- Tutorial 1: React Native Redux Boilerplate
- Tutorial 2: React Native Redux with CRUD operations
- How to build a React Native App using Firebase. (Part 1) (Setup)
- How to build a React Native App using Firebase. (Part 3) (Frontend)
- How to build a React Native App using Firebase. (Part 4)(Facebook Login)
- How to build a React Native App using Firebase. (Part 5)(CRUD)
- Tutorial 4: How to Build a Laravel 5.4 JWT-Powered Mobile App Authentication API
- Tutorial 5: How to Build a Laravel 5.4 JWT Authentication API with E-Mail Verification
- Tutorial 6 & 7: How to Build a Laravel 5.4 Administration Module with Role-based permissions using Entrust package
- Oct 25: Switched from using “Callbacks” to “Promises” — ( Folder structure changed, api.js file deleted, actions.js and reducer.js updated)
- May 11: Added Step 5 (CRUD)
- May 9: Updated ‘createUserWithEmailAndPassword’ and ‘login’ functions in api.js file. (This fixes Firebase 5.0 breaking change and immediately calls the createUser function instead of redirecting to the CompleteProfile View)