HOW TO CONNECT IONIC 5 TO FIREBASE

Firebase helps you build production ready apps without worrying much about backend logic and code maintenance.

It’s is a noSQL database which provides you with a scalable cloud database to store and sync data.

Applications (web, mobile and desktop) that seek to make use of firebase need to establish a connection to the platform. This will aid user authentications, crud functionalities, push notifications, crash reports and other features brought by firebase.

To establish a connection with ionic 5 and firebase, first create ionic angular application using ionic start as seen below.

$ ionic start fireBaseProject --type=angular

Check this if you don’t know how to start new ionic application.

Next, navigate inside the project folder and with npm, install angular/fire. AngularFire is the official angular library for firebase.

npm install firebase @angular/fire — save

Now we need to create new project in firebase console. Jump to firebase and create an account or login if you already a registered member.

Go to console at the top right hand corner and create a new project. Alternatively you can select any previous project to work with.

Add new project.

ionicTutorials is the fictional prpject name. Take a reasonal desired name to proceed and enable Google analytics for ‘this project’.

With the last step, accept all required terms and create project.

Firebase project is now created if the above was followed successfully and should be presented with a dashboard. To add firebase to the ionic app from the dashboard, select add firebase to your web app as shown below.

Give a nickname to the web app. Ionic Firebase Project is the name used here. Use a prefered name and register the app.

Lastly, copy firebase config from the dashboard and past into the ionic app code.

With the configuration script copied from firebase, past it into the environment file within the ionic app. Environment file is found within the project root folder of the ionic app.

export const environment = {
  production: true,
  firebase: {
    apiKey: "your_apiKey",
    authDomain: "your_authDomain",
    databaseURL: "your_databaseURL",
    projectId: "your_projectID",
    storageBucket: "your_storageBucket",
    messagingSenderId: "your_messagingSenderId"
  }
};

Next open environment.ts file within the same folder.

export const environment = {
  production: false,
  firebase: {
    apiKey: "your_apiKey",
    authDomain: "your_authDomain",
    databaseURL: "your_databaseURL",
    projectId: "your_projectId",
    storageBucket: "your_storageBucket",
    messagingSenderId: "your_messagingSenderId"
  }
};

Wrapping things up, open app.module.ts to import the insalled module @angular/fire.

//import what you need from firebase
import { AngularFireModule } from '@angular/fire';
//import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFireDatabaseModule } from '@angular/fire/database';
//import { AngularFireStorageModule } from '@angular/fire/storage';
//environment
import { environment } from '../environments/environment';

Job done and app fully connected to firebase.

Takeaway

Connecting a web app, mobile or desktop application to firebase is pretty straight forward and as seen from the opening pages of this article, firebase makes it easy to develop enterprise apps without worrying much about the backend code logic and maintenance which makes it one of the most widely used noSQL databases.

Firebase documentation is another way to further expand your knowledge on this noSQL database.