In this tutorial, we are going Learn how to handle Firebase Authentication In Angular using Google’s Cloud Firestore and Angularfire.
Most of the applications we build require some kind of Authentication and the simplest and fastest way to get started with that is by using the Firebase Firestore.
Firestore is a flexible, scalable database for mobile, web, and server development.
What you will learn
In this article, you will learn to develop a fully functioning authentication system in Angular using Firestore.
We will be using the official tool for Angular and Firebase integration – AngularFire.
AngularFire allows you to work with Cloud Firestore, the new flagship database for mobile app development.
It improves on the successes of Real-time Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales better than Realtime Database.
PREREQUISITES
In order to follow along with this tutorial please ensure that the latest version of Angular CLI is installed on your computer. You also need a Google account to be able to sign in to Firebase Console, where you will be creating the app which we will work with, in this tutorial.
Create a new Angular project using the Angular CLI
Incase you don’t have the angular CLI globally installed in your development machine, open up your terminal and type:
Open your terminal on your desktop and type this command to create a new Angular project:
This will ideally take some time, depending on how fast your internet is, so be patient.
Creating a Firebase project
Once the project is created, the next thing to do is create an app in Firebase, so navigate to the Firebase Console and then click on Add Project to create a new project.
Give it a name, I’ll call mine AngularAuth
, then click on Create Project afterward.
Now after the project has been created, you will be redirected to the project’s overview page, there you are meant to choose the option – Add Firebase to your web app.
On clicking that, you will get a pop up with all the info such as the API key that you need to be able to interact with your newly created Firebase app from the Angular application That’s it for now with Firebase, let’s go back to the Angular Project to create some components.
Creating Necessary components
We need a Homepage
, Login page
, and Sign Up page
to showcase how our Authentication is working. So we need to create an Angular component for each of these pages.
Now let us run our app to confirm everything is working.
Now, let’s go ahead and remove the default content that came with the new application. Navigate to src/app/app.component.html
to see the code. Select all content of this file and delete it. When you hit save and check your browser now, the page should be blank.
The app.component.html
file now becomes the new base/host for our homepage. Go ahead and add the selector of the homepage component there.
The app-homepage is the selector for the homepage component we created earlier.
Now we will install AngularFire
in our project:
Working with Firebase Authentication In Angular
Now, with the installed npm packages we need to configure our Firebase application to enable it to be able to communicate with your Angular application.
Click on the Authentication link and choose sign in method on the top tab after adding the firebaseConfig
to app.module.ts
file.
First, we will import the AngularFire modules to the app.module.ts
file. My app.module.ts
file looks like this after the new config is added.
Next, we will set up the Authentication methods, by clicking on the Authentication link described in the screenshot above. Once there, enable Sign-in for Email/Password and Google and then save. To enable for other providers you need an API key and API secret. Which can be easily generated by visiting the developers’ section of each of the providers.
Next, click on the Database on the sidebar at Firebase and then click on TRY FIRESTORE. Then click ENABLE.
Upon creation of the Database, check the Rules tab and make sure the content is similar to this:
Now let’s go ahead and use AngularFire and Firebase in our project. First, we need to create a service file which will serve all the methods that we need to login and logout.
Now, let’s go to the auth.service.ts file and add some code to it.
The first thing we need to do after creating a Service method is to add it to the providers array in the app.module.ts file, then after that we need to create routes for the components we created earlier and also create two more components, the EmailComponent and ProfileComponent. Use the same method we used to create components earlier to create those components.
Next, we’ll create the AppRoutingModule where we will create our routes. Create a file app-routing.module.ts in the src/app
folder, then fill it up with the following content.
Now let’s go ahead and work in our LoginComponent file navigate to the login/login.component.ts
, the method we will use there is the googleLogin()
.
And fill up the component file, login.component.html like so:
Before we go ahead and verify that everything is working, we need to change the content of the app.component.html file:
We should also add some styling to the form by editing the src/styles.css file, add the following css code to it:
Now the login page looks like this:
Log in with Google Account now works, next, we will implement Logging in with Email and then after that, work on the Signup form. Navigate to the email/email.component.ts
file:
Next, let’s navigate to the email/email.component.html file and add the following:
The login form now looks like this:
Next, let’s work on the Signup form and its component:
And then the template:
Lastly, the profile component which is the simplest of all, there we just need to add a Sign Out button that will take the user back to the Login Page.
Conclusion
There’s still room for improvement, we can check the state of the current user and guard the profile page so that a stranger can’t go to the User’s profile without Logging in. If you are reading this, you’ve probably navigated your way through this tutorial successfully. I hope you learned a few things about handling Firebase Authentication In Angular. Every article can be made better, so please leave your suggestions and contributions in the comments below. If you have questions about any of the steps, please do also ask in the comments section below.